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.

Download Report

Transcript 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.

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.