Академический Документы
Профессиональный Документы
Культура Документы
Chapter 11
Chapter Contents
Chapter Objectives
11.1 Introductory Example: A Trip to the
Aviary
11.2 Inheritance and Polymorphism
11.3 Example: Geological Classification
11.4 Example: An O-O Payroll Program
11.5 Graphical/Internet Java: A Function
Plotter
Chapter Objectives
Objects
name
color (some of the same name are of different
colors)
We
Weseek
seekaaprogram
program
to
they eat different things
tosimulate
simulatethis
this
collection
collection
they make different noises
some make multiple kinds of sounds
Design Sketch
Hierarchy
Bird
WalkingBird
call: ?
color:?
food:?
movement:walked
call: ?
color:?
food:?
movement:?
Goose
Ostrich
call: honk
color: gray
food: bugs
call: neek-neek
color: brown
food: grass
FlyingBird
call: ?
color:?
food:?
movement:flew
Parrot
Owl
call: Squawk
color:?
food: fruit
call:?
color:?
food:mice
TalkingParrot
...
Coding
color
food
movement
Subclasses
// FlyingBird provides subclass of Bird
abstract class FlyingBird extends Bird
{
public FlyingBird (String color,
String food)
{ super (color, food, "flying"); }
}
Values passed to Bird class constructor where used
to initialize attribute variables defined in class Bird
Subclasses
// Parrot provides subclass of FlyingBird
class Parrot extends FlyingBird
{
public Parrot(String color)
Note "is a" relationship: a
{ super(color, "fruit"); parrot
} is a flying bird.
public String getCall() Similarly, a Parrot is a Bird
{ return "Squawk!"; }
}
Movement attribute not an
argument for constructor, a parrot is
already specified as a flying bird
Aviary Class
Declaring subclasses
class B extends A
{ . . . }
means class B is a
specialization of class A
the "is a" relationship
exists
a B object is an A object
increasingly
general
"is a"
B
Subclass
increasingly
specialized
Inheritance
Other names:
attributes
methods
Inheritance
When we say
class TalkingParrot extends Parrot
{ }
Results of Inheritance
is unidirectional.
extends
this is legal
a Goose object "is a" Bird object
Contrast
Goose aGoose = new Bird("gray",
"walking", "bugs");
Polymorphism
Consider
Bird bird1 = new Parrot("green"),
bird2 = new TalkingParrot("red",phrases);
Polymorphism
Principles:
Java Hierarchy
define a method to do so
store the method within a class hierarchy to enable inheritance
1.
2.
1.
2.
Sedimentary
Igneous
Metamorphic
Objects
Object
Type
Kind
Name
A rock
Rock
varying
aRock
chalk
Chalk
constant
shale
Shale
constant
...
...
description
String
varying
aRock.getDescription()
Strategy
R ock
Ig n e o u s R o c k
B a s a lt
G r a n it e
O b s id ia n
S e d im e n t a r y R o c k
M e t a m o r p h ic R o c k
C h a lk . . .
M a r b le . . .
Operations
At the very least, each class should have:
A constructor
Accessor methods for class attributes
A toString() method to facilitate output
Also:
getDescription() method in each class
Attribute variable and accessor for which of the
three categories
Coding
4.
Coding
Main
aclass = (Rock)
Class.forName(rockString).newInstance();
returnsan
a actual
Class instance
object of this
creates
the class object sent to
representing
that to
class
class
and reference
it
newInstance()
rockString
passed to method
forName() method
Where:
StringVariable refers to a String containing
the name of a class
Purpose:
Objects
Object
Kind
Name
program
Type
PayrollGenerator
Employee seq
Employee[ ]
varying
employee
Input file
BufferedReader(
FileReader(
fileName))
varying
empFile
File name
String
varying
args[0]
Employee
Employee
varying
employee[i]
Managers
Manager
varying
...
...
...
Pay
...
double
varying
employee[i].pay()
Paycheck
Paycheck
varying
paycheck
Emp. name
String
varying
employee[i].name()
Analysis
Common attributes
employee
Hourly
employee
manager
Consultant
programmer
secretary
Hierarchy
O b je c t
P a y r o ll
G e n e ra to r
E m p lo y e e
S a la r ie d
E m p lo y e e
M anager
P ro g ra m m e r
P aych eck
H o u r ly
E m p lo y e e
C o n s u lt a n t
S e c re ta ry
Operations
Operation
Responsibility of:
PayrollGenerator,
Employee subclasses
Employee
3. Construct paycheck
Paycheck
Employee
5. Access ID number
Employee
Employee subclasses
File Format
Algorithm
1.
2.
Program Expansion
Behavior
Function Plotter
Function
(Clear)
Sine
X max 4.0
Y max
4.0
Cosine
Log10
Power
X min
4.0
Y min
Colors
Hierarchies
Polymorphism
Coding
FunctionPanel Class
constructor
accessor
mutator
clear() to erase function
the paintComponent() to draw the function