Chapter 4: Writing Classes
Chapter 4: Writing Classes
Chapter 4: Writing Classes
Ali Kassem
Presentation slides are copyright 2002 by John Lewis and William Loftus. All rights reserved.
Instructors using the textbook may use and modify these slides for pedagogical purposes.
Writing Classes
We've been using predefined classes. Now we will learn to
write our own classes to define objects
Ali Kassem 2
Objects
An object has:
• state - descriptive characteristics
Note that the behavior of the coin might change its state
Ali Kassem 3
Classes
A class is a blueprint of an object
Ali Kassem 4
Classes
The String class was provided for us by the Java
standard class library
But we can also write our own classes that define specific
objects that we need
Method declarations
The Coin Class
In our Coin class we could define the following data:
• face, an integer that represents the current face
Once the Coin class has been defined, we can use it again
in other programs as needed
Data Scope
The scope of data is the area in a program in which that
data can be used (referenced)
coin2
face 1
UML Diagrams
UML stands for the Unified Modeling Language
FlipRace Coin
1 2
face : int
main (args : String[]) : void
flip() : void
isHeads() : boolean
toString() : String
UML Diagrams
A UML object diagram consists of one or more instantiated
objects.
• internal - the variables the object holds and the methods that
make the object useful
• external - the services that an object provides and how the object
interacts
Ali Kassem 16
Encapsulation
An encapsulated object can be thought of as a black box
Client Methods
Data
Ali Kassem 17
Visibility Modifiers
In Java, we accomplish encapsulation through the
appropriate use of visibility modifiers
Ali Kassem 18
Visibility Modifiers
Members of a class that are declared with public visibility
can be accessed from anywhere
Ali Kassem 19
Visibility Modifiers
Methods that provide the object's services are usually
declared with public visibility so that they can be invoked
by clients
Ali Kassem 20
Visibility Modifiers
public private
Support other
Provide services
Methods methods in the
to clients
class
Driver Programs
A driver progam drives the use of other, more interesting
parts of a program
compute myMethod
myMethod();
Method Control Flow
The called method can be part of another class or object
obj.doIt(); helpMe();
Method Header
A method declaration begins with a method header
method
parameter list
name
return result;
} sum and result
are local data
return expression;
Ali Kassem 28
Parameters
Each time a method is called, the actual parameters in the
invocation are copied into the formal parameters
return result;
}
Local Data
Local variables can be declared inside a method
Ali Kassem 31
Overloading Methods
Method overloading is the process of using the same method
name for multiple methods
Ali Kassem 32
Overloading Methods
Version 1 Version 2
Invocation
println (String s)
println (int i)
println (double d)
and so on...
Ali Kassem 34
Overloading Methods
Constructors can be overloaded
Ali Kassem 35
Method Decomposition
A method should be relatively small, so that it can be
understood as a single entity
PigLatin PigLatinTranslator
1 1
writes
Author Book
Object Relationships
Some use associations occur between objects of the same
class
r3 = r1.add(r2);
StudentBody Student
1 2
- firstName : String
+ main (args : String[]) : void - lastName : String
- homeAddress : Address
- schoolAddress : Address
+ toString() : String
Address
- streetAddress : String
- city : String
- state : String
- zipCode : long
+ toString() : String
Applet Methods
In previous examples we've used the paint method of the
Applet class to draw on an applet
The start and stop methods are called when the applet
becomes active or inactive