Outline - IC
Download
Report
Transcript Outline - IC
Chapter 9 - Object-Oriented
Programming: Inheritance
Outline
9.1
9.2
9.3
9.4
9.5
9.6
9.7
Introduction
Superclasses and Subclasses
protected Members
Relationship between Superclasses and Subclasses
Case Study: Three-Level Inheritance Hierarchy
Constructors and Finalizers in Subclasses
Software Engineering with Inheritance
2003 Prentice Hall, Inc. All rights reserved.
1
2
9.1
Introduction
• Inheritance
– Software reusability
– Create new class from existing class
• Absorb existing class’s data and behaviors
• Enhance with new capabilities
– Subclass extends superclass
• Subclass
– More specialized group of objects
– Behaviors inherited from superclass
• Can customize
– Additional behaviors
2003 Prentice Hall, Inc. All rights reserved.
3
9.1
Introduction
• Class hierarchy
– Direct superclass
• Inherited explicitly (one level up hierarchy)
– Indirect superclass
• Inherited two or more levels up hierarchy
– Single inheritance
• Inherits from one superclass
– Multiple inheritance
• Inherits from multiple superclasses
– Java does not support multiple inheritance
2003 Prentice Hall, Inc. All rights reserved.
4
9.1
Introduction
• Abstraction
– Focus on commonalities among objects in system
• “is-a” vs. “has-a”
– “is-a”
• Inheritance
• subclass object treated as superclass object
• Example: Car is a vehicle
– Vehicle properties/behaviors also car properties/behaviors
– “has-a”
• Composition
• Object contains one or more objects of other classes as
members
• Example: Car has a steering wheel
2003 Prentice Hall, Inc. All rights reserved.
5
9.2 Superclasses and Subclasses
• Superclasses and subclasses
– Object of one class “is an” object of another class
• Example: Rectangle is quadrilateral.
– Class Rectangle inherits from class Quadrilateral
– Quadrilateral: superclass
– Rectangle: subclass
– Superclass typically represents larger set of objects than
subclasses
• Example:
– superclass: Vehicle
• Cars, trucks, boats, bicycles, …
– subclass: Car
• Smaller, more-specific subset of vehicles
2003 Prentice Hall, Inc. All rights reserved.
6
9.2 Superclasses and Subclasses (Cont.)
• Inheritance examples
Superclass
Student
Subclasses
GraduateStudent,
UndergraduateStudent
Shape
Circle, Triangle, Rectangle
Loan
CarLoan, HomeImprovementLoan,
MortgageLoan
Employee
Faculty, Staff
BankAccount CheckingAccount,
SavingsAccount
Fig. 9.1 Inheritance examples.
2003 Prentice Hall, Inc. All rights reserved.
7
9.2 Superclasses and Subclasses (Cont.)
• Inheritance hierarchy
– Inheritance relationships: tree-like hierarchy structure
– Each class becomes
• superclass
– Supply data/behaviors to other classes
OR
• subclass
– Inherit data/behaviors from other classes
2003 Prentice Hall, Inc. All rights reserved.
8
CommunityMember
Employee
Faculty
Administrator
Fig. 9.2
Student
Alumnus
Staff
Teacher
Inheritance hierarchy for university CommunityMembers.
2003 Prentice Hall, Inc. All rights reserved.
9
Shape
TwoDimensionalShape
Circle
Square
Triangle
Fig. 9.3
ThreeDimensionalShape
Sphere
Inheritance hierarchy for Shapes.
2003 Prentice Hall, Inc. All rights reserved.
Cube
Tetrahedron
10
9.3 protected Members
• protected access
– Intermediate level of protection between public and
private
– protected members accessible to
• superclass members
• subclass members
• Class members in the same package
– Subclass access superclass member
• Keyword super and a dot (.)
2003 Prentice Hall, Inc. All rights reserved.
11
9.4
Relationship between Superclasses
and Subclasses
• Superclass and subclass relationship
– Example: Point/circle inheritance hierarchy
• Point
– x-y coordinate pair
• Circle
– x-y coordinate pair
– Radius
2003 Prentice Hall, Inc. All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Fig. 9.4: Point.java
// Point class declaration represents
an x-y
pair.
Maintain
x-coordinate
and y-
12
Outline
coordinates as private
public class Point {
instance variables.
private int x; // x part of coordinate pair
private int y; // y part of coordinate pair
Implicit call to
// no-argument constructor
Object constructor
public Point()
{
// implicit call to Object constructor occurs here
}
// constructor
public Point( int xValue, int yValue )
{
// implicit call to Object constructor occurs here
x = xValue; // no need for validation
y = yValue; // no need for validation
}
Point.java
Lines 5-6
Maintain x- and ycoordinates as private
instance variables.
Line 11
Implicit call to Object
constructor
// set x in coordinate pair
public void setX( int xValue )
{
x = xValue; // no need for validation
}
2003 Prentice Hall, Inc.
All rights reserved.
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// return x from coordinate pair
public int getX()
{
return x;
}
13
Outline
Point.java
// set y in coordinate pair
public void setY( int yValue )
{
y = yValue; // no need for validation
}
// return y from coordinate pair
public int getY()
{
return y;
}
Lines 47-50
Override method
toString of class
Object.
Override method toString
of class Object
// return String representation of Point object
public String toString()
{
return "[" + x + ", " + y + "]";
}
} // end class Point
2003 Prentice Hall, Inc.
All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Fig. 9.5: PointTest.java
// Testing class Point.
import javax.swing.JOptionPane;
14
Outline
public class PointTest {
public static void main( String[] args )
{
Point point = new Point( 72, 115 ); // create
PointTest.java
Instantiate Point object
Line 9
Instantiate Point
Point object
object
// get point coordinates
String output = "X coordinate is " +Change
point.getX()
+
the value
"\nY coordinate is " + point.getY();
of point’s xand y- coordinates
point.setX( 10 );
point.setY( 20 );
// set x-coordinate
// set y-coordinate
// get String representation of new point value
output += "\n\nThe new location of point is " + point;
Lines 15-16
Change the value of
point’s x- and ycoordinates
Implicitly
call point’s
toString method
Line 19
Implicitly call point’s
toString method
JOptionPane.showMessageDialog( null, output ); // display output
System.exit( 0 );
} // end main
} // end class PointTest
2003 Prentice Hall, Inc.
All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Fig. 9.6: Circle.java
// Circle class contains x-y coordinate pair and radius.
public class Circle {
private int x;
private int y;
private double radius;
15
Outline
Maintain x-y coordinates and
radiusof
as Circle's
privatecenter
x-coordinate
y-coordinate
Circle's center
instance of
variables.
//
//
// Circle's radius
Lines 5-7
Maintain x- and ycoordinates and radius
as private instance
variables.
// no-argument constructor
public Circle()
{
// implicit call to Object constructor occurs here
}
// constructor
public Circle( int xValue, int yValue, double radiusValue )
{
// implicit call to Object constructor occurs here
x = xValue; // no need for validation
y = yValue; // no need for validation
setRadius( radiusValue );
}
// set x in coordinate pair
public void setX( int xValue )
{
x = xValue; // no need for validation
}
Circle.java
Lines 25-28
Note code similar to
Point code.
Note code similar to Point
code.
2003 Prentice Hall, Inc.
All rights reserved.
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// return x from coordinate pair
public int getX()
{
return x;
}
// set y in coordinate pair
public void setY( int yValue )
{
y = yValue; // no need for validation
}
16
Outline
Note code similar to Point Circle.java
code.
Lines 31-47
Note code similar to
Point code.
Line 51
Ensure non-negative
value for radius.
// return y from coordinate pair
public int getY()
{
return y;
}
// set radius
public void setRadius( double radiusValue )
{
radius = ( radiusValue < 0.0 ? 0.0 : radiusValue );
}
// return radius
public double getRadius()
{
return radius;
}
Ensure non-negative value for
radius.
2003 Prentice Hall, Inc.
All rights reserved.
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// calculate and return diameter
public double getDiameter()
{
return 2 * radius;
}
17
Outline
Circle.java
// calculate and return circumference
public double getCircumference()
{
return Math.PI * getDiameter();
}
// calculate and return area
public double getArea()
{
return Math.PI * radius * radius;
}
// return String representation of Circle object
public String toString()
{
return "Center = [" + x + ", " + y + "]; Radius = " + radius;
}
} // end class Circle
2003 Prentice Hall, Inc.
All rights reserved.
18
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Outline
// Fig. 9.7: CircleTest.java
// Testing class Circle.
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
CircleTest.java
Create Circle object.
public class CircleTest {
public static void main( String[] args )
{
Circle circle = new Circle( 37, 43, 2.5 ); // create Circle object
// get Circle's initial x-y coordinates and radius
String output = "X coordinate is " + circle.getX() +
"\nY coordinate is " + circle.getY() +
"\nRadius is " + circle.getRadius();
circle.setX( 35 );
circle.setY( 20 );
circle.setRadius( 4.25 );
// set new x-coordinate
// set new y-coordinate
// set new radius
// get String representation of
output += "\n\nThe new location
circle.toString();
Explicitly call circle’s
new toString
circle
value
Use
setmethod
methods to modify
and radius
of circleinstance
are\n" +
private
variable.
Line 10
Create Circle object
Lines 17-19
Use set methods to
modify private
instance variable
Line 23
Explicitly call circle’s
toString method
// format floating-point values with 2 digits of precision
DecimalFormat twoDigits = new DecimalFormat( "0.00" );
2003 Prentice Hall, Inc.
All rights reserved.
19
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// get Circle's diameter
output += "\nDiameter is " +
twoDigits.format( circle.getDiameter() );
Outline
CircleTest.java
// get Circle's circumference
output += "\nCircumference is " +
twoDigits.format( circle.getCircumference() );
Use get methods to
29-37
obtainLines
circle’s
diameter,
circumference and area.
Use get methods to
// get Circle's area
obtain circle’s
output += "\nArea is " + twoDigits.format( circle.getArea() );
diameter,
circumference and
JOptionPane.showMessageDialog( null, output ); // display output
area.
System.exit( 0 );
} // end main
} // end class CircleTest
2003 Prentice Hall, Inc.
All rights reserved.
20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Fig. 9.8: Circle2.java
// Circle2 class inherits from Point.
Class Circle2
extends class Point.
public class Circle2 extends Point {
private double radius; // Circle2's radius
Maintain private instance
// no-argument constructor
variable radius.
public Circle2()
{
// implicit call to Point constructor occurs here
}
// constructor
Attempting to access superclass
public Circle2( int xValue, int yValue, double radiusValue )
Point’s private instance
{
variables
and y results in syntax
// implicit call to Point constructor
occursxhere
x = xValue; // not allowed: x private
in Point
errors.
y = yValue; // not allowed: y private in Point
setRadius( radiusValue );
}
// set radius
public void setRadius( double radiusValue )
{
radius = ( radiusValue < 0.0 ? 0.0 : radiusValue );
}
Outline
Circle2.java
Line 4
Class Circle2
extends class Point.
Line 5
Maintain private
instance variable
radius.
Lines 17-18
Attempting to access
superclass Point’s
private instance
variables x and y
results in syntax
errors.
2003 Prentice Hall, Inc.
All rights reserved.
21
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// calculate and return diameter
public double getDiameter()
{
return 2 * radius;
}
// calculate and return circumference
public double getCircumference()
{
return Math.PI * getDiameter();
}
// calculate and return area
public double getArea()
{
return Math.PI * radius * radius;
}
Outline
Circle2.java
Line 56
Attempting to access
superclass Point’s
private instance
variables x and y
results in syntax
errors.
Attempting to access superclass
// return String representation of Circle object
Point’s private instance
public String toString()
variables x and y results in
{
// use of x and y not allowed: x and y privatesyntax
in Point
errors.
return "Center = [" + x + ", " + y + "]; Radius = " + radius;
}
} // end class Circle2
2003 Prentice Hall, Inc.
All rights reserved.
22
Outline
Circle2.java:17: x has private access in Point
x = xValue; // not allowed: x private in Point
^
Circle2.java:18: y has private access in Point
y = yValue; // not allowed: y private in Point
^
Circle2.java:56: x has private access in Point
return "Center = [" + x + ", " + y + "]; Radius = " + radius;
^
Circle2.java:56: y has private access in Point
return "Center = [" + x + ", " + y + "]; Radius = " + radius;
^
4 errors
Circle2.java
output
Attempting to access
superclass Point’s
private instance
variables x and y
results in syntax
errors.
Attempting to access
superclass Point’s
private instance variables
x and y results in syntax
errors.
2003 Prentice Hall, Inc.
All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Fig. 9.9: Point2.java
// Point2 class declaration represents an x-y coordinate pair.
public class Point2 {
protected int x; // x part of
protected int y; // y part of
Maintain x- and ycoordinates as protected
instancepair
variables, accessible
coordinate
to subclasses.
coordinate
pair
// no-argument constructor
public Point2()
{
// implicit call to Object constructor occurs here
}
23
Outline
Point2.java
Lines 5-6
Maintain x- and ycoordinates as
protected instance
variables, accessible
to subclasses.
// constructor
public Point2( int xValue, int yValue )
{
// implicit call to Object constructor occurs here
x = xValue; // no need for validation
y = yValue; // no need for validation
}
// set x in coordinate pair
public void setX( int xValue )
{
x = xValue; // no need for validation
}
2003 Prentice Hall, Inc.
All rights reserved.
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// return x from coordinate pair
public int getX()
{
return x;
}
24
Outline
Point2.java
// set y in coordinate pair
public void setY( int yValue )
{
y = yValue; // no need for validation
}
// return y from coordinate pair
public int getY()
{
return y;
}
// return String representation of Point2 object
public String toString()
{
return "[" + x + ", " + y + "]";
}
} // end class Point2
2003 Prentice Hall, Inc.
All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Fig. 9.10: Circle3.java
Circle3
inherits from
// Circle3 class inherits from Point2 and has Class
access
to Point2
// protected members x and y.
class Point2.
Maintain
private instance
variables radius.
public class Circle3 extends Point2 {
private double radius; // Circle3's radius
// no-argument constructor
public Circle3()
{
// implicit call to Point2 constructor occurs here
}
Implicitly calls superclass’s
default constructor.
// constructor
public Circle3( int xValue, int Modify
yValue,inherited
double radiusValue
)
instance
{
variables x and y, declared
// implicit call to Point2 constructor occurs here
protected in superclass
x = xValue; // no need for validation
Point2.
y = yValue; // no need for validation
setRadius( radiusValue );
}
// set radius
public void setRadius( double radiusValue )
{
radius = ( radiusValue < 0.0 ? 0.0 : radiusValue );
}
25
Outline
Circle3.java
Line 5
Class Circle3
inherits from class
Point2.
Line 6
Maintain private
instance variables
radius.
Lines 11 and 17
Implicitly call
superclass’s default
constructor.
Lines 18-19
Modify inherited
instance variables x
and y, declared
protected in
superclass Point2.
2003 Prentice Hall, Inc.
All rights reserved.
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// return radius
public double getRadius()
{
return radius;
}
26
Outline
Circle3.java
// calculate and return diameter
public double getDiameter()
{
return 2 * radius;
}
Line 56
Access inherited
instance variables x
and y, declared
protected in
superclass Point2.
// calculate and return circumference
public double getCircumference()
{
return Math.PI * getDiameter();
}
// calculate and return area
public double getArea()
{
return Math.PI * radius * radius;
}
Access inherited instance
variables x and y, declared
protected in superclass
Point2.
// return String representation of Circle3 object
public String toString()
{
return "Center = [" + x + ", " + y + "]; Radius = " + radius;
}
} // end class Circle3
2003 Prentice Hall, Inc.
All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Fig. 9.11: CircleTest3.java
// Testing class Circle3.
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
27
Outline
Circletest3.java
Line 11
public class CircleTest3 {
Create Circle3 object.
Create Circle3 object. Lines 14-15
public static void main( String[] args )
Use inherited get methods
{
to access inherited
// instantiate Circle object
protected instance
Circle3 circle = new Circle3( 37, 43, 2.5 );
variables
x and y.to
Use inherited
get methods
Useaccess
Circle3
get method
variables
x andtoy.
// get Circle3's initial x-y coordinates and radius
inherited
protected
Lineinstance
16
access
private
String output = "X coordinate is " + circle.getX() +
instance
variables
x and y.
Use
Circle3
get
variables.
"\nY coordinate is " + circle.getY() +
method to access
"\nRadius is " + circle.getRadius();
private instance
variables.
circle.setX( 35 );
// set new x-coordinate
Lines 18-19
circle.setY( 20 );
// set new y-coordinate
Use inherited set methods
circle.setRadius( 4.25 ); // set new radius
Use inherited set methods to
to modify inherited
modify inherited
protected data x and y.
// get String representation of new circleprotected
value
data
x
and
y.
Line 20
Use Circle3 set method to
output += "\n\nThe new location and radius of circle are\n" +
Use Circle3 set
modify private data
circle.toString();
method to modify
radius.
private data radius.
2003 Prentice Hall, Inc.
All rights reserved.
28
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// format floating-point values with 2 digits of precision
DecimalFormat twoDigits = new DecimalFormat( "0.00" );
// get Circle's diameter
output += "\nDiameter is " +
twoDigits.format( circle.getDiameter() );
Outline
Circletest3.jav
a
// get Circle's circumference
output += "\nCircumference is " +
twoDigits.format( circle.getCircumference() );
// get Circle's area
output += "\nArea is " + twoDigits.format( circle.getArea() );
JOptionPane.showMessageDialog( null, output ); // display output
System.exit( 0 );
} // end method main
} // end class CircleTest3
2003 Prentice Hall, Inc.
All rights reserved.
29
9.4
Relationship between Superclasses
and Subclasses (Cont.)
• Using protected instance variables
– Advantages
• subclasses can modify values directly
• Slight increase in performance
– Avoid set/get function call overhead
– Disadvantages
• No validity checking
– subclass can assign illegal value
• Implementation dependent
– subclass methods more likely dependent on superclass
implementation
– superclass implementation changes may result in subclass
modifications
• Fragile (brittle) software
2003 Prentice Hall, Inc. All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Fig. 9.12: Point3.java
// Point class declaration represents an x-y coordinate pair.
Better software-engineering
practice: private over
protected
coordinate
pair when possible.
public class Point3 {
private int x; // x part of
private int y; // y part of coordinate pair
// no-argument constructor
public Point3()
{
// implicit call to Object constructor occurs here
}
30
Outline
Point3.java
Lines 5-6
Better softwareengineering practice:
private over
protected when
possible.
// constructor
public Point3( int xValue, int yValue )
{
// implicit call to Object constructor occurs here
x = xValue; // no need for validation
y = yValue; // no need for validation
}
// set x in coordinate pair
public void setX( int xValue )
{
x = xValue; // no need for validation
}
2003 Prentice Hall, Inc.
All rights reserved.
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// return x from coordinate pair
public int getX()
{
return x;
}
31
Outline
Point3.java
// set y in coordinate pair
public void setY( int yValue )
{
y = yValue; // no need for validation
}
Line 49
Invoke public
methods to access
private instance
variables.
// return y from coordinate pair
public int getY()
{
return y;
}
Invoke public methods to access
object
private instance variables.
// return String representation of Point3
public String toString()
{
return "[" + getX() + ", " + getY() + "]";
}
} // end class Point3
2003 Prentice Hall, Inc.
All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Fig. 9.13: Circle4.java
Class Circle4
// Circle4 class inherits from Point3 and accesses Point3's
class Point3.
// private x and y via Point3's public methods.
public class Circle4 extends Point3 {
private double radius;
inherits from
Maintain private instance
variable radius.
// Circle4's radius
// no-argument constructor
public Circle4()
{
// implicit call to Point3 constructor occurs here
}
// constructor
public Circle4( int xValue, int yValue, double radiusValue )
{
super( xValue, yValue ); // call Point3 constructor explicitly
setRadius( radiusValue );
}
32
Outline
Circle4.java
Line 5
Class Circle4
inherits from class
Point3.
Line 7
Maintain private
instance variable
radius.
// set radius
public void setRadius( double radiusValue )
{
radius = ( radiusValue < 0.0 ? 0.0 : radiusValue );
}
2003 Prentice Hall, Inc.
All rights reserved.
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// return radius
public double getRadius()
{
return radius;
}
33
Outline
Circle4.java
// calculate and return diameter
public double getDiameter()
{
return 2 * getRadius();
}
// calculate and return circumference
public double getCircumference()
{
return Math.PI * getDiameter();
}
Invoke method getRadius
rather than directly accessing
instance variable radius.
// calculate and return area
public double getArea()
{
return Math.PI * getRadius() * getRadius();
Redefine
}
Line 37, 49 and 55
Invoke method
getRadius rather
than directly accessing
instance variable
radius.
Lines 53-56
Redefine class
Point3’s method
toString.
class Point3’s
method toString.
// return String representation of Circle4 object
public String toString()
{
return "Center = " + super.toString() + "; Radius = " + getRadius();
}
} // end class Circle4
2003 Prentice Hall, Inc.
All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Fig. 9.14: CircleTest4.java
// Testing class Circle4.
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
34
Outline
Circletest4.java
Line 11
public class CircleTest4 {
Create Circle4 object.
Create Circle4 object. Lines 14 and 15
public static void main( String[] args )
Use inherited get methods
{
to access inherited
// instantiate Circle object
private
instance
Use inherited get
methods
to
Circle4 circle = new Circle4( 37, 43, 2.5 );
x and y.
access inheritedvariables
private
Linex16
instance
variables
and y. to
// get Circle4's initial x-y coordinates and radius
Use Circle4
get method
Use
Circle4
get
String output = "X coordinate is " + circle.getX() +
access private
instance
method to access
"\nY coordinate is " + circle.getY() +
variable radius.private instance
"\nRadius is " + circle.getRadius();
variable radius.
Lines 18-19
circle.setX( 35 );
// set new x-coordinate
Use inherited seta
circle.setY( 20 );
// set new y-coordinate
Use inherited seta methods to methods to modify
circle.setRadius( 4.25 ); // set new radius
modify inherited private inherited private
instance
variables
x and y.to instance variables x and y.
// get String representation of new circle
value
Use
Circle4
set method
Line 20
output += "\n\nThe new location and radius
of circle
are\n"
+
modify
private
instance
Use Circle4 set
circle.toString();
variable radius.
method to modify
private instance
variable radius.
2003 Prentice Hall, Inc.
All rights reserved.
35
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// format floating-point values with 2 digits of precision
DecimalFormat twoDigits = new DecimalFormat( "0.00" );
// get Circle's diameter
output += "\nDiameter is " +
twoDigits.format( circle.getDiameter() );
Outline
Circletest4.jav
a
// get Circle's circumference
output += "\nCircumference is " +
twoDigits.format( circle.getCircumference() );
// get Circle's area
output += "\nArea is " + twoDigits.format( circle.getArea() );
JOptionPane.showMessageDialog( null, output ); // display output
System.exit( 0 );
} // end main
} // end class CircleTest4
2003 Prentice Hall, Inc.
All rights reserved.
36
9.5
Case Study: Three-Level Inheritance
Hierarchy
• Three level point/circle/cylinder hierarchy
– Point
• x-y coordinate pair
– Circle
• x-y coordinate pair
• Radius
– Cylinder
• x-y coordinate pair
• Radius
• Height
2003 Prentice Hall, Inc. All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Fig. 9.15: Cylinder.java
// Cylinder class inherits from Circle4.
Maintain private instance
variable height.
public class Cylinder extends Circle4 {
private double height; // Cylinder's height
Class Cylinder extends
// no-argument constructor
class Circle4.
public Cylinder()
{
// implicit call to Circle4 constructor occurs here
}
// constructor
public Cylinder( int xValue, int yValue, double radiusValue,
double heightValue )
{
super( xValue, yValue, radiusValue ); // call Circle4 constructor
setHeight( heightValue );
}
37
Outline
Cylinder.java
Line 4
Class Cylinder
extends class
Circle4.
Line 5
Maintain private
instance variable
height.
// set Cylinder's height
public void setHeight( double heightValue )
{
height = ( heightValue < 0.0 ? 0.0 : heightValue );
}
2003 Prentice Hall, Inc.
All rights reserved.
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// get Cylinder's height
public double getHeight()
{
return height;
}
38
Outline
Redefine superclass
Circle4’s method
Invoke
superclass
calculate
Cylinder
area
getArea
to return
Circle4’s getArea
Cylinder surface area.
method using keyword super.
Cylinder.java
// override Circle4 method getArea to
public double getArea()
{
return 2 * super.getArea() + getCircumference() * getHeight();
}
Line 34 and 42
Redefine superclass
Circle4’s method
getArea to return
Cylinder surface area.
// calculate Cylinder volume
public double getVolume()
{
return super.getArea() * getHeight();
}
Line 36
Invoke superclass
Circle4’s getArea
method using keyword
super.
Redefine class Circle4’s
method toString.
Cylinder Invoke
objectsuperclass
Circle4’s toString
method using keyword super.
// return String representation of
public String toString()
{
return super.toString() + "; Height = " + getHeight();
}
} // end class Cylinder
Lines 46-49
Redefine class
Circle4’s method
toString.
Line 48
Invoke superclass
Circle4’s toString
method using keyword
super.
2003 Prentice Hall, Inc.
All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Fig. 9.16: CylinderTest.java
// Testing class Cylinder.
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
39
Outline
CylinderTest.java
Lines 14 and 15
public class CylinderTest {
Invoke indirectly
inherited Point3 get
public static void main( String[] args )
methods.
{
Line 16
// create Cylinder object
directly inherited
inherited
Cylinder cylinder = new Cylinder( 12, 23, 2.5, 5.7 ); Invoke indirectlyInvoke
Circle4 get method.
Point3 get methods.
Line 16get method.
Invoke Cylinder
// get Cylinder's initial x-y coordinates,
radius
and height
Invoke
directly
inherited
Invoke Cylinder get
String output = "X coordinate is " + cylinder.getX()
Circle4 get+method.
method.
"\nY coordinate is " + cylinder.getY() + "\nRadius is " +
Lines 18-19
cylinder.getRadius() + "\nHeight is " + cylinder.getHeight();
Invoke indirectly
inherited Point3 set
cylinder.setX( 35 );
// set new x-coordinate
Invoke indirectly inherited
methods.
cylinder.setY( 20 );
// set new y-coordinate
Line 20
Point3 set methods.
cylinder.setRadius( 4.25 );
// set new radius
Invoke directly inherited
cylinder.setHeight( 10.75 ); // set new Invoke
height directly inherited
Circle4 set method.
Circle4 set method.
Line 21
Invoke Cylinder set
// get String representation of new cylinder value
Invoke Cylinder set
method.
output +=
method.
"\n\nThe new location, radius and height of cylinder are\n" +
Line 26
cylinder.toString();
Invoke overridden
toString method.
Invoke overridden
method.
toString method.
2003 Prentice Hall, Inc.
All rights reserved.
40
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// format floating-point values with 2 digits of precision
DecimalFormat twoDigits = new DecimalFormat( "0.00" );
Outline
// get Cylinder's diameter
output += "\n\nDiameter is " +
twoDigits.format( cylinder.getDiameter() );
CylinderTest.ja
va
// get Cylinder's circumference
output += "\nCircumference is " +
twoDigits.format( cylinder.getCircumference() );
Line 40
Invoke overridden
getArea method.
// get Cylinder's area
output += "\nArea is " + twoDigits.format( cylinder.getArea() );
Invoke overridden
// get Cylinder's volume
output += "\nVolume is " + twoDigits.format( cylinder.getVolume()
);
method.
getArea
JOptionPane.showMessageDialog( null, output ); // display output
System.exit( 0 );
} // end main
} // end class CylinderTest
2003 Prentice Hall, Inc.
All rights reserved.
41
9.6
Constructors and Finalizers in
Subclasses
• Instantiating subclass object
– Chain of constructor calls
• subclass constructor invokes superclass constructor
– Implicitly or explicitly
• Base of inheritance hierarchy
– Last constructor called in chain is Object’s constructor
– Original subclass constructor’s body finishes executing
last
– Example: Point3/Circle4/Cylinder hierarchy
• Point3 constructor called second last (last is
Object constructor)
• Point3 constructor’s body finishes execution
second (first is Object constructor’s body)
2003 Prentice Hall, Inc. All rights reserved.
42
9.6
Constructors and Destructors in
Derived Classes
• Garbage collecting subclass object
– Chain of finalize method calls
• Reverse order of constructor chain
• Finalizer of subclass called first
• Finalizer of next superclass up hierarchy next
– Continue up hierarchy until final superreached
• After final superclass (Object) finalizer, object
removed from memory
2003 Prentice Hall, Inc. All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Fig. 9.17: Point.java
// Point class declaration represents an x-y coordinate pair.
43
Outline
public class Point {
private int x; // x part of coordinate pair
private int y; // y part of coordinate pair
Point.java
Lines 12, 22 and 28
Constructor and
finalizer output
messages to
demonstrate method
call order.
Constructor and finalizer
output messages to
demonstrate method call order.
// no-argument constructor
public Point()
{
// implicit call to Object constructor occurs here
System.out.println( "Point no-argument constructor: " + this );
}
// constructor
public Point( int xValue, int yValue )
{
// implicit call to Object constructor occurs here
x = xValue; // no need for validation
y = yValue; // no need for validation
System.out.println( "Point constructor: " + this );
}
// finalizer
protected void finalize()
{
System.out.println( "Point finalizer: " + this );
}
2003 Prentice Hall, Inc.
All rights reserved.
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// set x in coordinate pair
public void setX( int xValue )
{
x = xValue; // no need for validation
}
44
Outline
Point.java
// return x from coordinate pair
public int getX()
{
return x;
}
// set y in coordinate pair
public void setY( int yValue )
{
y = yValue; // no need for validation
}
// return y from coordinate pair
public int getY()
{
return y;
}
// return String representation of Point4 object
public String toString()
{
return "[" + getX() + ", " + getY() + "]";
}
} // end class Point
2003 Prentice Hall, Inc.
All rights reserved.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Fig. 9.18: Circle.java
// Circle5 class declaration.
45
Outline
public class Circle extends Point {
private double radius;
Circle.java
// Circle's radius
// no-argument constructor
public Circle()
{
// implicit call to Point constructor occurs here
System.out.println( "Circle no-argument constructor: " + this );
}
// constructor
public Circle( int xValue, int yValue, double radiusValue )
{
super( xValue, yValue ); // call Point constructor
setRadius( radiusValue );
Lines 12, 21 and 29
Constructor and
finalizer output
messages to
demonstrate method
Constructor and finalizer
call order.
output messages to
demonstrate method call order.
System.out.println( "Circle constructor: " + this );
}
// finalizer
protected void finalize()
{
System.out.println( "Circle finalizer: " + this );
super.finalize();
// call superclass finalize method
}
2003 Prentice Hall, Inc.
All rights reserved.
46
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// set radius
public void setRadius( double radiusValue )
{
radius = ( radiusValue < 0.0 ? 0.0 : radiusValue );
}
Outline
Circle.java
// return radius
public double getRadius()
{
return radius;
}
// calculate and return diameter
public double getDiameter()
{
return 2 * getRadius();
}
// calculate and return circumference
public double getCircumference()
{
return Math.PI * getDiameter();
}
2003 Prentice Hall, Inc.
All rights reserved.
47
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// calculate and return area
public double getArea()
{
return Math.PI * getRadius() * getRadius();
}
Outline
Circle.java
// return String representation of Circle5 object
public String toString()
{
return "Center = " + super.toString() + "; Radius = " + getRadius();
}
} // end class Circle
2003 Prentice Hall, Inc.
All rights reserved.
48
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Fig. 9.19: ConstructorFinalizerTest.java
// Display order in which superclass and subclass
// constructors and finalizers are called.
ConstructorFina
lizerTest.java
public class ConstructorFinalizerTest {
public static void main( String args[] )
{
Point point;
Circle circle1, circle2;
point = new Point( 11, 22 );
System.out.println();
circle1 = new Circle( 72, 29, 4.5 );
Line 12
Point object goes in and out
Point object goes in
of scope immediately.
and out of scope
immediately.
Instantiate two Circle
objects to demonstrate
order
Lines 15
and 18
of subclass and superclass
Instantiate two
constructor/finalizer
method
Circle
objects to
calls.
demonstrate order of
System.out.println();
circle2 = new Circle( 5, 7, 10.67 );
point = null;
circle1 = null;
circle2 = null;
Outline
// mark for garbage collection
// mark for garbage collection
// mark for garbage collection
subclass and
superclass
constructor/finalizer
method calls.
System.out.println();
2003 Prentice Hall, Inc.
All rights reserved.
49
26
27
28
29
30
System.gc();
// call the garbage collector
Outline
} // end main
} // end class ConstructorFinalizerTest
Point constructor: [11, 22]
Point constructor: Center = [72, 29]; Radius = 0.0
Circle constructor: Center = [72, 29]; Radius = 4.5
Point constructor: Center = [5, 7]; Radius = 0.0
Circle constructor: Center = [5, 7]; Radius = 10.67
Point finalizer: [11, 22]
Circle finalizer: Center = [72, 29]; Radius = 4.5
Point finalizer: Center = [72, 29]; Radius = 4.5
Circle finalizer: Center = [5, 7]; Radius = 10.67
Point finalizer: Center = [5, 7]; Radius = 10.67
ConstructorFina
lizerTest.java
Subclass Circle constructor
body executes after superclass
Point4’s constructor
finishes execution.
Finalizer for Circle object
called in reverse order of
constructors.
2003 Prentice Hall, Inc.
All rights reserved.
50
9.9
Software Engineering with Inheritance
• Customizing existing software
– Inherit from existing classes
• Include additional members
• Redefine superclass members
• No direct access to superclass’s source code
– Link to object code
– Independent software vendors (ISVs)
• Develop proprietary code for sale/license
– Available in object-code format
• Users derive new classes
– Without accessing ISV proprietary source code
2003 Prentice Hall, Inc. All rights reserved.