Professional Documents
Culture Documents
Object-Oriented Software
Design and Java Programming
1
Object-Oriented Design
2
Object Oriented Programming
Programmer thinks about and defines
the attributes and behavior of
objects.
Abstraction
Polymorphism
Inheritance
Encapsulation
4
Objects to Classes
5
Class: Object Types
6
Creating an object
Instantiation
7
Superclass and Subclass
• Inheritance enables us to define a new class
(called a subclass) that inherits the properties of
an already existing class.
8
Defining Objects
9
Identity, State, Behavior
10
Identity, State, Behavior
• The state of an object encompasses all of the
(static) properties of the object plus the current
(dynamic) values of each of these properties
• Values
• Current floor
• Number of coins deposited
• The number of minutes since the last
hour
12
Identity, State, Behavior
• Behavior is how an object acts and reacts,
in terms of state changes and interactions
with other objects.
• An operation is some action that one
object performs upon another in order to
elicit a reaction.
• We will use the word method to describe
object behavior in C++.
• Invoking a method causes the behavior to
take place.
13
Classes
• Classes are the definitions (or
blueprints) used to create objects. I’d
say: descriptions of objects.
15
Object example
• Notice that all objects are of the same type. All objects
are cars!
16
Classes and Objects
• An object is an instance of exactly one class
• Corvette can not be an instance of a car class
and an instance of a plane class at the same
time.
17
Classes
• Once a class is defined you can create as
many instances of the class (objects from
the class) as you would like.
18
Defining a class
• Properties are variables which describe the
essential characteristics of an object.
• Properties of a car: color, model, make, how many
doors, transmission type, direction of movement, etc.
19
Instance variables
• The class definition will include parameter definitions
(properties) that represent data about a particular object,
instance variables.
• Example, Joe's car may have 4 gallons of gas in it while
John's car has 10 gallons.
• The amount of gas in each car may change without
affecting the amount of gas in the any other cars.
• All instances (objects) of a class will have a set of instance
variables that are specific to that individual object.
• The combination of the values of these instance variables
is known as the object’s state.
20
Instance variables
Car
MaxSpeed
21
Class variables
22
Class variables
Car
MaxSpeed
MaxSpeed=155
23
Class variables
24
Class variables
Car
MaxSpeed
MaxSpeed=155
NumCars = 3
25
Messages
Audi 6 • turnOnHazard()
28
Instance methods
• Each class can have methods that are specific to
each object, called instance methods.
29
Messages and Methods
Car
MaxSpeed
MaxSpeed=155
NumCars = 3
turnOnHazard()
addGass(amount)
30
Methods
• It is also possible that you want information
from an object; in this case, you would define
a method that sends (returns) a message back
to the requester containing that information.
31
Messages and Methods
Car
MaxSpeed
GasLevel
MaxSpeed=155
NumCars = 3 turnOnHazard()
addGass(amount) getGasLevel():GasLevel
addGass(amount) getGasLevel():GasLeve 32
l
Class methods
• Class methods are used to get or
manipulate information about all objects
created from the class.
• Typically, class methods are changing
class variables. For example:
• Each time we move the car in or out of the
garage, we need to add/subtract one to the
number of cars: carIn( ) & carOut( )
• Also, we may want to know how many cars
are actually in the garage: getNumCars( )
33
Messages and Methods
Car
MaxSpeed
GasLevel carIn()
MaxSpeed=155
NumCars = 3 carOut()
turnOnHazard() getNumCars():NumCars
addGass(amount) getGasLevel():GasLevel
38
Polymorphism
39
Inheritance
40
Inheritance
• All classes in Java are organized into a
class hierarchy.
• The highest level classes are very general
and the lower level classes are more
specific.
• The lower level classes are based upon the
higher level classes and inherit instance
variables and methods from those higher
level class. They also may contain their
own (new) instance variables and methods
beyond the higher level class definition. 41
Inheritance
• A higher level class is called a superclass;
a lower level class is called a subclass.
42
Inheritance Example
Base class is shape, represents the
abstract notion of a shape.
Derived classes:
rectangle
circle
triangle.
44
Inheritance
Car BMW
MaxSpeed Model
GasLevel Color
MaxSpeed=155 EngineSize
turnOnHazard() MaxSpeed
addGass(amount) GasLevel
getGasLevel():GasLevel
turnOnHazard()
addGass(amount)
getGasLevel():GasLevel
45
Inheritance
• Now let's define two new classes. One for
the Z3 and another for the 3 Series
Sedan.
• What might be some of the differences
between the two classes?
• Number of doors (3, 5)
• Roof (soft or hardtop)
• Therefore, we add variables NumDoors
and Roof
46
Inheritance
Car BMW
MaxSpeed Model
GasLevel Color
MaxSpeed=155 EngineSize
turnOnHazard() MaxSpeed
addGass(amount) GasLevel
getGasLevel():GasLevel
turnOnHazard()
addGass(amount)
getGasLevel():GasLevel
Z3 3 series
Model Model
Color Color
EngineSize EngineSize
Roof NumDoors
MaxSpeed MaxSpeed
GasLevel GasLevel
turnOnHazard() turnOnHazard()
addGass(amount) addGass(amount)
getGasLevel():GasLevel getGasLevel():GasLevel
47
Views of the class
• A class can be viewed as a sort of contract that
specifies what instances of the class can, and
cannot do
• It is possible to distinguish between the outside
and inside view of a class
• The interface of a class provides its outside
view and emphasizes the abstraction
• The implementation of a class is its inside view
48
Access
50
Protected Class
members/methods
We've already seen private and public.
51
Special Member Functions
53
Accessing class methods
54
static methods
55
Static Data Members
56
Static data member example
class foo {
foo() {
foocount++;
}
}
...
57
Two Programming Paradigms
FUNCTION OBJECT
Operations
FUNCTION Data
OBJECT
OBJECT Operations
FUNCTION
Operations Data
Data
58
Object-Oriented Programming
Language Features
1. Data abstraction
2. Inheritance of properties
59
OOP Terms Java Equivalents
60
What is an object?
OBJECT
set of methods
Operations (public member functions)
61
Inheritance Hierarchy Among
Vehicles
vehicle
car bicycle
two-door four-door
64
TimeCard Class
TimeCard has a Time object
Private data:
Punch id
timeStamp
Print Private data:
Set
.
. hrs
Increment
. mins
Write
TimeCard . secs
.
.
TimeCard
65
Constructor Rules for Derived Classes
66
Order in Which Constructors are
Executed
Given a class X,
67
End of Lecture
68