2. Writing Classes
The programs we’ve written in previous examples
have used classes defined in the Java standard
class library
Now we will begin to design programs that rely on
classes that we write ourselves
True object-oriented programming is based on
defining classes that represent objects with well-
defined characteristics and functionality
3. Graphical Representation of a Class
The notation we used here is based on the industry standard notation
called UML, which stands for Unified Modeling Language.
A UML Class Diagram
Class Name
Variables
Method
Type of data
Access
Type of
return
value
A class can
contain data
declarations and
method
declarations
5. Object Design Questions
What role will the object perform?
What data or information will it need?
Look for nouns.
Which actions will it take?
Look for verbs.
What interface will it present to other
objects?
These are public methods.
What information will it hide from other
objects?
These are private.
6. Design Specification for a Rectangle
Class Name: Rectangle
Role: To represent a geometric rectangle
States (Information or instance variables)
Length: A variable to store rectangle’s length (private)
Width: A variable to store rectangle's width
(private)
Behaviors (public methods)
- Rectangle(): A constructor method to set a
rectangle’s length and width
- calculateArea(): A method to calculate a
rectangle’s area
7. UML Design Specification
UML Class Diagram
Class Name
What data does it need?
What behaviors
will it perform?
Public
methods
Hidden
information
Instance variables -- memory locations
used for storing the information needed.
Methods -- blocks of code used to
perform a specific task.