Transcript Outline

Chapter 9 - Object-Oriented Programming
9.10
9.11
9.12
9.13
9.14
9.15
9.16
9.17
9.18
9.19
9.20
9.21
9.22
Introduction to Polymorphism
Type Fields and switch Statements
Dynamic Method Binding
final Methods and Classes
Abstract Superclasses and Concrete Classes
Polymorphism Examples
Case Study: A Payroll System Using Polymorphism
New Classes and Dynamic Binding
Case Study: Inheriting Interface and Implementation
Case Study: Creating and Using Interfaces
Inner Class Definitions
Notes on Inner Class Definitions
Type-Wrapper Classes for Primitive Types
 2002 Prentice Hall. All rights reserved.
Page 1
9.10 Introduction to Polymorphism
• Polymorphism
– Helps build extensible systems
– Programs generically process objects as superclass objects
• Can add classes to systems easily
– Classes must be part of generically processed hierarchy
 2002 Prentice Hall. All rights reserved.
Page 2
9.11 Type Fields and Switch Statements
• One way to deal with objects of many different types is to use a
switch-based system
– Determine appropriate action for object
• Based on object’s type
– Error prone
• Programmer can forget to make appropriate type test
• Adding and deleting switch statements
 2002 Prentice Hall. All rights reserved.
Page 3
9.12 Dynamic Method Binding
• Dynamic method binding
–
–
–
–
Implements polymorphic processing of objects
Use superclass reference to refer to subclass object
Program chooses “correct” method in subclass
Program takes on a simplified appearance; less branching
logic more sequential code
• Facilitates testing, debugging and program maintenance
 2002 Prentice Hall. All rights reserved.
Page 4
9.12 Dynamic Method Binding (cont.)
• For example,
– Superclass Shape
– Subclasses Circle, Rectangle and Square
– Each class draws itself according to type of class
• Shape has method draw
• Each subclass overrides method draw
• Call method draw of superclass Shape
– Program determines dynamically which subclass draw
method to invoke
 2002 Prentice Hall. All rights reserved.
Page 5
9.13 final Methods and Classes
• final method
– Cannot be overridden in subclass
– Compiler can optimize the program by removing calls to
final methods and replacing them with the expanded code
at each method call location – inlining the code
• final class
– Cannot be superclass (cannot be extended)
• A class cannot inherit from a final classes
• Every method is implicitly final
 2002 Prentice Hall. All rights reserved.
Page 6
9.14 Abstract Superclasses and Concrete
Classes
• Abstract classes
– Objects cannot be instantiated
– Too generic to define real objects
• TwoDimensionalShape
– Provides superclass from which other classes may inherit
• Normally referred to as abstract superclasses
• Concrete classes
– Classes from which objects are instantiated
– Provide specifics for instantiating objects
• Square, Circle and Triangle
 2002 Prentice Hall. All rights reserved.
Page 7
9.15 Polymorphism Examples
• Video game
– Superclass GamePiece
• Contains method drawYourself
– Subclasses Martian, Venutian, LaserBeam, etc.
• Override method drawYourself
– Martian draws itself with antennae
– LaserBeam draws itself as bright red beam
– This is polymorphism
– Easily extensible
• Suppose we add class Mercurian
– Class Mercurian inherits superclass GamePiece
– Overrides method drawYourself
 2002 Prentice Hall. All rights reserved.
Page 8
9.16 Case Study: A Payroll System Using
Polymorphism
• Abstract methods and polymorphism
– Abstract superclass Employee
• Method earnings applies to all employees
• Person’s earnings dependent on type of Employee
– Concrete Employee subclasses declared final
• Boss
• CommissionWorker
• PieceWorker
• HourlyWorker
 2002 Prentice Hall. All rights reserved.
Page 9
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.16: Employee.java
// Abstract base class Employee.
abstract class cannot be instantiated
public abstract class Employee {
private String firstName;
private String lastName;
// constructor
public Employee( String first,
{
firstName = first;
lastName = last;
}
// get first name
public String getFirstName()
{
return firstName;
}
Outline
Employee.java
abstract class can have instance data
Line 4
and nonabstract methods for subclasses
abstract class
cannot be instantiated
String last )
Lines 5-6 and 16-30
abstract class can have constructors for
abstract class can
subclasses to initialize inherited data
have instance data and
nonabstract
methods for subclasses
// get last name
public String getLastName()
{
return lastName;
}
Lines 9-13
abstract class can
have constructors for
subclasses to initialize
inherited data
public String toString()
{
return firstName + ' ' + lastName;
}
Page 10
32
33
34
35
36
37
// Abstract method that must be implemented for each
// derived class of Employee from which objects
// are instantiated.
public abstract double earnings();
}
// end class Employee
Subclasses must implement
abstract method
Outline
Employee.java
Line 35
Subclasses must
implement abstract
method
Page 11
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
32
// Fig. 9.17: Boss.java
// Boss class derived from Employee.
Boss is an Employee subclass
public final class Boss extends Employee {
private double weeklySalary;
Boss.java
Boss inherits Employee’s public
Line 4
methods (except for constuctor)
Boss is an Employee
double salary )
subclass
// constructor for class Boss
public Boss( String first, String last,
{
super( first, last ); // call superclass constructor
setWeeklySalary( salary );
Explicit call to Employee
}
constructor using super
// set Boss's salary
public void setWeeklySalary( double salary )
{
weeklySalary = ( salary > 0 ? salary : 0 );
}
// get Boss's pay
public double earnings()
{
return weeklySalary;
}
// end class Boss
Line 4
Boss inherits
Employee’s public
methods (except for
constuctor)
Required to implement Employee’s
Line 10
method earnings (polymorphism)
Explicit call to
Employee constructor
using super
// get String representation of Boss's name
public String toString()
{
return "Boss: " + super.toString();
}
}
Outline
Lines 21-24
Required to implement
Employee’s method
earnings
(polymorphism)
Page 12
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
CommissionWorker is
// Fig. 9.18: CommissionWorker.java
// CommissionWorker class derived from Employee
Employee subclass
an
public final class CommissionWorker extends Employee {
private double salary;
// base salary per week
private double commission; // amount per item sold
private int quantity;
// total items sold for week
// constructor for class CommissionWorker
public CommissionWorker( String first, String last,
double salary, double commission, int quantity )
{
super( first, last ); // call superclass constructor
setSalary( salary );
Explicit call to Employee
setCommission( commission );
constructor using super
setQuantity( quantity );
}
Outline
CommissionWorker
.java
Line 4
CommissionWorker
is an Employee
subclass
Line 13
Explicit call to
Employee constructor
using super
// set CommissionWorker's weekly base salary
public void setSalary( double weeklySalary )
{
salary = ( weeklySalary > 0 ? weeklySalary : 0 );
}
// set CommissionWorker's commission
public void setCommission( double itemCommission )
{
commission = ( itemCommission > 0 ? itemCommission : 0 );
}
Page 13
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// set CommissionWorker's quantity sold
public void setQuantity( int totalSold
Required to) implement Employee’s
{
method earnings; this implementation
quantity = ( totalSold > 0 ? totalSold : 0 );
differs from that in Boss
}
// determine CommissionWorker's earnings
public double earnings()
{
return salary + commission * quantity;
}
// get String representation of CommissionWorker's name
public String toString()
{
return "Commission worker: " + super.toString();
}
}
Outline
CommissionWorker
.java
Lines 38-41
Required to implement
Employee’s method
earnings; this
implementation differs
from that in Boss
// end class CommissionWorker
Page 14
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
32
33
34
PieceWorker is an
// Fig. 9.19: PieceWorker.java
// PieceWorker class derived from Employee
Employee subclass
public final class PieceWorker extends Employee {
private double wagePerPiece; // wage per piece output
private int quantity;
// output for week
// constructor for class PieceWorker
public PieceWorker( String first, String last,
double wage, int numberOfItems )
{
super( first, last ); // call superclass constructor
setWage( wage );
Explicit call to Employee
setQuantity( numberOfItems);
constructor using super
}
// set PieceWorker's wage
public void setWage( double wage )
{
wagePerPiece = ( wage > 0 ? wage : 0 );
}
// set number of items output
public void setQuantity( int numberOfItems )
{
quantity = ( numberOfItems > 0 ? numberOfItems : 0 );
}
// determine PieceWorker's earnings
public double earnings()
{
return quantity * wagePerPiece;
}
Outline
PieceWorker.java
Line 4
PieceWorker is an
Employee subclass
Line 12
Explicit call to
Employee constructor
using super
Lines 30-33
Implementation of
Employee’s method
earnings; differs
from that of Boss and
CommissionWorker
Implementation of Employee’s method
earnings; differs from that of Boss
and CommissionWorker
Page 15
35
36
37
38
39
40
public String toString()
{
return "Piece worker: " + super.toString();
}
}
Outline
PieceWorker.java
// end class PieceWorker
Page 16
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
32
// Fig. 9.20: HourlyWorker.java
// Definition of class HourlyWorker
HourlyWorker is an
Employee subclass
public final class HourlyWorker extends Employee {
private double wage;
// wage per hour
private double hours; // hours worked for week
// constructor for class HourlyWorker
public HourlyWorker( String first, String last,
double wagePerHour, double hoursWorked )
{
super( first, last );
// call superclass constructor
setWage( wagePerHour );
Explicit call to Employee
setHours( hoursWorked );
}
constructor using super
Outline
HourlyWorker.java
Line 4
PieceWorker is an
Employee subclass
Line 12
Explicit call to
Employee constructor
using super
// Set the wage
public void setWage( double wagePerHour )
{
wage = ( wagePerHour > 0 ? wagePerHour : 0 );
}
Line 31
Implementation of
Employee’s method
Implementation of Employee’s
method
earnings;
differs from
earnings; differs from
of other
thatthat
of other
Employee
Employee subclasses
subclasses
// Set the hours worked
public void setHours( double hoursWorked )
{
hours = ( hoursWorked >= 0 && hoursWorked < 168 ?
hoursWorked : 0 );
}
// Get the HourlyWorker's pay
public double earnings() { return wage * hours; }
Page 17
33
34
35
36
37
38
public String toString()
{
return "Hourly worker: " + super.toString();
}
}
Outline
HourlyWorker.java
// end class HourlyWorker
Page 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
28
29
30
31
Outline
// Fig. 9.21: Test.java
// Driver for Employee hierarchy
Test.java
// Java core packages
import java.text.DecimalFormat;
// Java extension packages
import javax.swing.JOptionPane;
public class Test {
Line 15
Test cannot
Test cannot instantiate Employee instantiate Employee
but can reference one
but can reference one
// test Employee hierarchy
public static void main( String args[] )
{
Employee employee; // superclass reference
String output = "";
Lines 18-28
Instantiate one instance
each
Instantiate
oneofinstance
Employeeeach
subclasses
of Employee
subclasses
Boss boss = new Boss( "John", "Smith", 800.0 );
CommissionWorker commisionWorker =
new CommissionWorker(
"Sue", "Jones", 400.0, 3.0, 150 );
PieceWorker pieceWorker =
new PieceWorker( "Bob", "Lewis", 2.5, 200 );
HourlyWorker hourlyWorker =
new HourlyWorker( "Karen", "Price", 13.75, 40 );
DecimalFormat precision2 = new DecimalFormat( "0.00" );
Page 19
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
// Employee reference to a Boss
employee = boss;
Use Employee to reference Boss
output += employee.toString() + " earned $" +
precision2.format( employee.earnings() ) + "\n" +
boss.toString() + " earned $" +
precision2.format( boss.earnings() ) + "\n"; Method
Outline
Test.java
Line 33
employee.earnings
Use Employee to
dynamically binds
to method
reference
Boss
// Employee reference to a CommissionWorker
boss.earnings
employee = commissionWorker;
Line 36
output += employee.toString() + " earned $" +
Method
precision2.format( employee.earnings() ) + "\n" +
commissionWorker.toString() + " earned $" +
employee.earning
precision2.format(
s dynamically binds to
commissionWorker.earnings() ) + "\n";
Do same for CommissionWorker
method
// Employee reference to a PieceWorker
and PieceWorker
boss.earnings
employee = pieceWorker;
output += employee.toString() + " earned $" +
precision2.format( employee.earnings() ) + "\n" +
pieceWorker.toString() + " earned $" +
precision2.format( pieceWorker.earnings() ) + "\n";
Lines 41-55
Do same for
CommissionWorker
and PieceWorker
Page 20
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
Outline
// Employee reference to an HourlyWorker
employee = hourlyWorker;
output += employee.toString() + " earned $" +
precision2.format( employee.earnings() ) + "\n" +
hourlyWorker.toString() + " earned $" +
precision2.format( hourlyWorker.earnings() ) + "\n";
JOptionPane.showMessageDialog( null, output,
"Demonstrating Polymorphism",
JOptionPane.INFORMATION_MESSAGE );
Test.java
Lines 58-63
Repeat for
HourlyWorker
Repeat for HourlyWorker
System.exit( 0 );
}
}
// end class Test
Page 21
9.17 New Classes and Dynamic Binding
• Dynamic binding (late binding)
– Object’s type need not be know at compile time
– At run time, call is matched with method of called object
 2002 Prentice Hall. All rights reserved.
Page 22
9.18 Case Study: Inheriting Interface and
Implementation
• Point, Circle, Cylinder hierarchy
– Modify by including abstract superclass Shape
• Demonstrates polymorphism
• Contains abstract method getName
– Each subclass must implement method getName
• Contains (nonabstract) methods area and volume
– Return 0 by default
– Each subclass overrides these methods
 2002 Prentice Hall. All rights reserved.
Page 23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Fig. 9.22: Shape.java
Shape
// Definition of abstract base class Shape
cannot be instantiated
public abstract class Shape extends Object {
// return shape's area
public double area()
{
return 0.0;
}
// return shape's volume
public double volume()
{
return 0.0;
}
Shape.java
Line 4
Shape cannot be
instantiated
abstract class can
have nonabstract
methods for subclasses
// abstract method must be defined by concrete subclasses
// to return appropriate shape name
public abstract String getName();
}
Outline
// end class Shape
Concrete subclasses must
implement method getName
Lines 7-16
abstract class can
have nonabstract
methods for subclasses
Line 20
Concrete subclasses
must implement
method getName
Page 24
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.23: Point.java
// Definition of class Point
Point inherits Shape’s
public methods
public class Point extends Shape {
protected int x, y; // coordinates of the Point
// no-argument constructor
public Point()
{
setPoint( 0, 0 );
}
protected members prevent
clients from direct access (unless
clients are Point subclasses or
are in same package)
// constructor
public Point( int xCoordinate, int yCoordinate )
{
setPoint( xCoordinate, yCoordinate );
}
// set
public
{
x =
y =
}
x and y coordinates of Point
void setPoint( int xCoordinate, int yCoordinate )
Outline
Point.java
Line 4
Point inherits
Shape’s public
methods
Line 5
protected members
prevent clients from
direct access (unless
clients are Point
subclasses or are in
same package)
xCoordinate;
yCoordinate;
// get x coordinate
public int getX()
{
return x;
}
Page 25
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Outline
// get y coordinate
public int getY()
{
return y;
}
Point.java
// convert point into String representation
public String toString()
{
return "[" + x + ", " + y + "]";
}
Implementation
// return shape name
public String getName()
{
return "Point";
}
}
// end class Point
of Shape’s
method getName
Lines 45-48
Implementation of
Shape’s method
getName
*** Note ***
Point does not override methods
area and volume, because points
have neither area nor volume
Page 26
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
32
33
34
35
// Fig. 9.24: Circle.java
// Definition of class Circle
public class Circle extends Point {
protected double radius;
Circle inherits variables/methods
from Point and Shape
// inherits from Point
// no-argument constructor
public Circle()
{
// implicit call to superclass constructor here
setRadius( 0 );
}
// constructor
public Circle( double circleRadius, int xCoordinate,
int yCoordinate )
{
// call superclass constructor
Methods
super( xCoordinate, yCoordinate );
setRadius( circleRadius );
Outline
Circle.java
Line 4
Circle inherits
variables/methods from
Point and Shape
Lines 5 and 24-34
Methods for
reading/setting
protected value
for reading/setting
protected value
}
// set radius of Circle
public void setRadius( double circleRadius )
{
radius = ( circleRadius >= 0 ? circleRadius : 0 );
}
// get radius of Circle
public double getRadius()
{
return radius;
}
Page 27
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// calculate area of Circle
public double area()
{
return Math.PI * radius * radius;
}
Outline
Circle.java
Override method area but not method volume
Lines 37-40
// convert Circle to a String represention
(circles do not have volume)
Override method area
public String toString()
but not method
{
return "Center = " + super.toString() +
volume (circles do not
"; Radius = " + radius;
have volume)
}
Implementation of Shape’s
method getName
// return shape name
Lines 50-53
public String getName()
Implementation of
{
Shape’s method
return "Circle";
}
getName
}
// end class Circle
Page 28
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.25: Cylinder.java
// Definition of class Cylinder.
Cylinder inherits variables
and methods from Point,
Circle and Shape
public class Cylinder extends Circle {
protected double height; // height of Cylinder
// no-argument constructor
public Cylinder()
{
// implicit call to superclass constructor here
setHeight( 0 );
}
Outline
Cylinder.java
Line 4
Cylinder inherits
variables and methods
from Point, Circle
and Shape
// constructor
public Cylinder( double cylinderHeight,
double cylinderRadius, int xCoordinate,
int yCoordinate )
{
// call superclass constructor
super( cylinderRadius, xCoordinate, yCoordinate );
setHeight( cylinderHeight );
}
// set height of Cylinder
public void setHeight( double cylinderHeight )
{
height = ( cylinderHeight >= 0 ? cylinderHeight : 0 );
}
Page 29
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
Outline
// get height of Cylinder
public double getHeight()
{
return height;
}
Cylinder.java
// calculate area of Cylinder (i.e., surface area)
public double area()
{
return 2 * super.area() + 2 * Math.PI * radius * height;
}
// calculate volume of Cylinder
public double volume()
{
return super.area() * height;
}
Override methods area
and volume
Lines 38-47
Override methods
area and volume
Lines 56-59
Implementation of
Shape’s method
getName
// convert Cylinder to a String representation
public String toString()
{
return super.toString() + "; Height = " + height;
}
// return shape name
public String getName()
{
return "Cylinder";
}
}
Implementation of Shape’s
method getName
// end class Cylinder
Page 30
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
Outline
// Fig. 9.26: Test.java
// Class to test Shape, Point, Circle, Cylinder hierarchy
Test.java
// Java core packages
import java.text.DecimalFormat;
// Java extension packages
import javax.swing.JOptionPane;
public class Test {
Instantiate one instance
each of Shape subclasses
// test Shape hierarchy
public static void main( String args[] )
{
// create shapes
Point point = new Point( 7, 11 );
Circle circle = new Circle( 3.5, 22, 8 );
Cylinder cylinder = new Cylinder( 10, 3.3, 10, 10 );
// create Shape array
Shape arrayOfShapes[] = new Shape[ 3 ];
Lines 16-18
Instantiate one instance
each of Shape
subclasses
Lines 21-30
Create three Shapes to
reference each subclass
object
Create three Shapes to
reference each subclass object
// aim arrayOfShapes[ 0 ] at subclass Point object
arrayOfShapes[ 0 ] = point;
// aim arrayOfShapes[ 1 ] at subclass Circle object
arrayOfShapes[ 1 ] = circle;
// aim arrayOfShapes[ 2 ] at subclass Cylinder object
arrayOfShapes[ 2 ] = cylinder;
Page 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
// get name and String representation of each shape
String output =
point.getName() + ": " + point.toString() + "\n" +
circle.getName() + ": " + circle.toString() + "\n" +
cylinder.getName() + ": " + cylinder.toString();
DecimalFormat precision2 = new DecimalFormat( "0.00" );
// loop through arrayOfShapes and get name,
// area and volume of each shape in arrayOfShapes
for ( int i = 0; i < arrayOfShapes.length; i++ ) {
output += "\n\n" + arrayOfShapes[ i ].getName() +
": " + arrayOfShapes[ i ].toString() +
"\nArea = " +
precision2.format( arrayOfShapes[ i ].area() ) +
"\nVolume = " +
precision2.format( arrayOfShapes[ i ].volume() );
}
JOptionPane.showMessageDialog( null, output,
"Demonstrating Polymorphism",
JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
Outline
Test.java
Line 43
Dynamically
bind bind
Dynamically
method method
getName
getName
Line 46bind method
Dynamically
Dynamically
area
for Circlebind
and
method
area
for
Cylinder objects
Circle and
Cylinder objects
Line 48
Dynamically Dynamically
bind method bind
volume for Cylinder
object for
method volume
Cylinder object
}
}
// end class Test
Page 32
Outline
Test.java
Page 33
9.19 Case Study: Creating and Using Interfaces
• Use interface Shape
– interface Used instead of an abstract class when there
are no instance variables or default method implementations to
inherit
• Interface
– Definition begins with interface keyword
– Classes implement an interface
– Class must define every method in the interface with the number
of arguments and return type specified in the interface
• If any methods are undefined, the class is abstract and ust be
declared so
– Contains public abstract methods
• Classes (that implement the interface) must implement these
methods
– A class can implement more than one interface
 2002 Prentice Hall. All rights reserved.
Page 34
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Outline
// Fig. 9.27: Shape.java
// Definition of interface Shape
public interface Shape {
// calculate area
public abstract double area();
// calculate volume
public abstract double volume();
// return shape name
public abstract String getName();
Classes that implement Shape
must implement these methods
Shape.java
Lines 7-13
Classes that
implement Shape
must implement these
methods
}
Page 35
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
Outline
// Fig. 9.28: Point.java
// Definition of class Point
public class Point extends Object implements Shape {
protected int x, y; // coordinates of the Point
// no-argument constructor
public Point()
{
setPoint( 0, 0 );
}
Point.java
Line 4
Point implements
interface Shape
Point implements interface Shape
// constructor
public Point( int xCoordinate, int yCoordinate )
{
setPoint( xCoordinate, yCoordinate );
}
// Set
public
{
x =
y =
}
x and y coordinates of Point
void setPoint( int xCoordinate, int yCoordinate )
xCoordinate;
yCoordinate;
// get x coordinate
public int getX()
{
return x;
}
Page 36
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
62
Outline
// get y coordinate
public int getY()
{
return y;
}
Point.java
// convert point into String representation
public String toString()
{
return "[" + x + ", " + y + "]";
}
Lines 45-60
Implement methods
specified by interface
Shape
// calculate area
public double area()
{
return 0.0;
}
// calculate volume
public double volume()
{
return 0.0;
}
Implement methods specified
by interface Shape
// return shape name
public String getName()
{
return "Point";
}
}
// end class Point
Page 37
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
32
33
34
35
Outline
// Fig. 9.29: Circle.java
// Definition of class Circle
public class Circle extends Point {
protected double radius;
// inherits from Point
Circle.java
Line 4
Circle inherits variables/methods
// no-argument constructor
Circle inherits
public Circle()
from Point, including method
variables/methods from
{
implementations of Shape
// implicit call to superclass constructor here
Point, including
setRadius( 0 );
method implemen}
tations of Shape
// constructor
public Circle( double circleRadius, int xCoordinate,
int yCoordinate )
{
// call superclass constructor
super( xCoordinate, yCoordinate );
setRadius( circleRadius );
}
// set radius of Circle
public void setRadius( double circleRadius )
{
radius = ( circleRadius >= 0 ? circleRadius : 0 );
}
// get radius of Circle
public double getRadius()
{
return radius;
}
Page 38
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// calculate area of Circle
public double area()
{
return Math.PI * radius * radius;
}
Outline
Circle.java
Override method toString
// convert Circle to a String represention
public String toString()
{
return "Center = " + super.toString() +
"; Radius = " + radius;
}
// return shape name
public String getName()
{
return "Circle";
}
}
Lines 43-47
Override method
toString
Lines 37-40 and 50-53
Override methods area and
Override methods
getName but not method volume
area and getName
but not method
volume
// end class Circle
Page 39
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
32
33
34
35
// Fig. 9.30: Cylinder.java
// Definition of class Cylinder.
public class Cylinder extends Circle {
protected double height; // height of Cylinder
Circle inherits variables/methods
// no-argument constructor
public Cylinder()
from Point and Circle and
{
method implementations of Shape
// implicit call to superclass constructor here
setHeight( 0 );
}
// constructor
public Cylinder( double cylinderHeight,
double cylinderRadius, int xCoordinate,
int yCoordinate )
{
// call superclass constructor
super( cylinderRadius, xCoordinate, yCoordinate );
Outline
Cylinder.java
Line 4
Circle inherits
variables/methods from
Point and Circle
and method
implementations of
Shape
setHeight( cylinderHeight );
}
// set height of Cylinder
public void setHeight( double cylinderHeight )
{
height = ( cylinderHeight >= 0 ? cylinderHeight : 0 );
}
// get height of Cylinder
public double getHeight()
{
return height;
}
Page 40
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
// calculate area of Cylinder (i.e., surface area)
public double area()
{
return 2 * super.area() + 2 * Math.PI * radius * height;
}
// calculate volume of Cylinder
public double volume()
{
return super.area() * height;
}
Override method toString
Outline
Cylinder.java
Lines 50-53
Override method
toString
Line 38-59
Override methods area,
Override methods
volume and getName
area, volume and
getName
// convert Cylinder to a String representation
public String toString()
{
return super.toString() + "; Height = " + height;
}
// return shape name
public String getName()
{
return "Cylinder";
}
}
// end class Cylinder
Page 41
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.31: Test.java
// Test Point, Circle, Cylinder hierarchy with interface Shape.
Test.java
// Java core packages
import java.text.DecimalFormat;
// Java extension packages
import javax.swing.JOptionPane;
Outline
Fig. 9.31 is identical
to Fig. 9.26
Fig. 9.31 is identical to
Fig. 9.26
public class Test {
// test Shape hierarchy
public static void main( String args[] )
{
// create shapes
Point point = new Point( 7, 11 );
Circle circle = new Circle( 3.5, 22, 8 );
Cylinder cylinder = new Cylinder( 10, 3.3, 10, 10 );
// create Shape array
Shape arrayOfShapes[] = new Shape[ 3 ];
// aim arrayOfShapes[ 0 ] at subclass Point object
arrayOfShapes[ 0 ] = point;
// aim arrayOfShapes[ 1 ] at subclass Circle object
arrayOfShapes[ 1 ] = circle;
// aim arrayOfShapes[ 2 ] at subclass Cylinder object
arrayOfShapes[ 2 ] = cylinder;
Page 42
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
// get name and String representation of each shape
String output =
point.getName() + ": " + point.toString() + "\n" +
circle.getName() + ": " + circle.toString() + "\n" +
cylinder.getName() + ": " + cylinder.toString();
Outline
Test.java
DecimalFormat precision2 = new DecimalFormat( "0.00" );
// loop through arrayOfShapes and get name,
// area and volume of each shape in arrayOfShapes
for ( int i = 0; i < arrayOfShapes.length; i++ ) {
output += "\n\n" + arrayOfShapes[ i ].getName() +
": " + arrayOfShapes[ i ].toString() +
"\nArea = " +
precision2.format( arrayOfShapes[ i ].area() ) +
"\nVolume = " +
precision2.format( arrayOfShapes[ i ].volume() );
}
JOptionPane.showMessageDialog( null, output,
"Demonstrating Polymorphism",
JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
}
// end class Test
Page 43
Outline
Test.java
Output is identical to
that of Fig. 9.26
Output is identical to
that of Fig. 9.26
Page 44
9.19 Interfaces
• Interfaces can also define a set of constants that can be
used in many class definitions
public interface
public static
public static
public static
}
Constants{
final int ONE=1;
final int TWO=2;
final int THREE=3;
• Classes that implement interface Constants can use
ONE, TWO and THREE anywhere in the class definition
• Classes that import the interface can refer to them as
Constants.ONE, Constants.TWO
• As there are no methods in this interface, a class that
implements it is nt required to provide any method
implementations
 2002 Prentice Hall. All rights reserved.
Page 45
9.20 Inner Class Definitions
• Inner classes
– Class is defined inside another class body
– Frequently used with GUI handling
• Declare ActionListener inner class
• GUI components can register ActionListeners for events
– Button events, key events, etc.
– An inner class is allowed to access directly all the instance
variables and methods of the outer class
– An inner class defined in a method is allowed access directly
to all the instance variables and methods of the outer class
object that defined it and any final local variables in the
method
 2002 Prentice Hall. All rights reserved.
Page 46
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
32
33
34
35
Outline
// Fig. 9.32: Time.java
// Time class definition.
// Java core packages
import java.text.DecimalFormat;
// This class maintains the time in 24-hour format
public class Time extends Object {
private int hour;
// 0 - 23
private int minute;
// 0 - 59
private int second;
// 0 - 59
Time.java
Line 8
Same Time class used
in Chapter 8
// Time constructor initializes each instance variable
// to zero. Ensures that Time object starts in a
// consistent state.
public Time()
Same Time class
{
setTime( 0, 0, 0 );
used in Chapter 8
}
// Set a new time value using universal time. Perform
// validity checks on the data. Set invalid values to zero.
public void setTime( int hour, int minute, int second )
{
setHour( hour );
setMinute( minute );
setSecond( second );
}
// validate and set hour
public void setHour( int h )
{
hour = ( ( h >= 0 && h < 24 ) ? h : 0 );
}
Page 47
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
62
63
64
65
Outline
// validate and set minute
public void setMinute( int m )
{
minute = ( ( m >= 0 && m < 60 ) ? m : 0 );
}
Time.java
Mutator and accessor
methods
// validate and set second
public void setSecond( int s )
{
second = ( ( s >= 0 && s < 60 ) ? s : 0 );
}
// get hour
public int getHour()
{
return hour;
}
Mutator and
accessor methods
// get minute
public int getMinute()
{
return minute;
}
// get second
public int getSecond()
{
return second;
}
Page 48
66
67
68
69
70
71
72
73
74
75
76
77
78
// convert to String in standard-time format
public String toString()
{
DecimalFormat twoDigits = new DecimalFormat( "00" );
return ( ( getHour() == 12 || getHour() == 0 ) ?
12 : getHour() % 12 ) + ":" +
twoDigits.format( getMinute() ) + ":" +
twoDigits.format( getSecond() ) +
( getHour() < 12 ? " AM" : " PM" );
Outline
Time.java
Lines 67-76
Override method
java.lang.Object
.toString
}
}
// end class Time
Override method
java.lang.Object.toString
Page 49
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
Outline
// Fig. 9.33: TimeTestWindow.java
// Demonstrating the Time class set and get methods
// Java core packages
import java.awt.*;
import java.awt.event.*;
JFrame provides basic window
attributes and behaviors
// Java extension packages
import javax.swing.*;
public class TimeTestWindow extends JFrame {
private Time time;
private JLabel hourLabel, minuteLabel, secondLabel;
private JTextField hourField, minuteField,
secondField, displayField;
JFrame
private JButton exitButton;
TimeTestWindow.j
ava
Line 11
JFrame provides basic
window attributes and
behaviors
Line 19
(unlike JApplet)
JFrame (unlike
has constructor JApplet) has
// set up GUI
public TimeTestWindow()
Instantiate
{
super( "Inner Class Demonstration" );
Time object
constructor
Line 23
Instantiate Time object
time = new Time();
Line 26
Instantiate object of
inner-class that
implements
Instantiate object ofActionListener
innerclass that implements
ActionListener
// create an instance of inner class ActionEventHandler
ActionEventHandler handler = new ActionEventHandler();
// set up GUI
Container container = getContentPane();
container.setLayout( new FlowLayout() );
Page 50
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
62
63
64
65
hourLabel = new JLabel( "Set Hour" );
hourField = new JTextField( 10 );
hourField.addActionListener( handler );
container.add( hourLabel );
container.add( hourField );
minuteLabel = new JLabel( "Set minute" );
minuteField = new JTextField( 10 );
minuteField.addActionListener( handler );
container.add( minuteLabel );
container.add( minuteField );
secondLabel = new JLabel( "Set Second" );
secondField = new JTextField( 10 );
secondField.addActionListener( handler );
container.add( secondLabel );
container.add( secondField );
Outline
TimeTestWindow.j
ava
Line 34, 40, 46 and 55
Register ActionEventHandler with
GUI components
Register
ActionEventHandler
with GUI components
displayField = new JTextField( 30 );
displayField.setEditable( false );
container.add( displayField );
exitButton = new JButton( "Exit" );
exitButton.addActionListener( handler );
container.add( exitButton );
}
// end constructor
// display time in displayField
public void displayTime()
{
displayField.setText( "The time is: " + time );
}
Page 51
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
Outline
// create TimeTestWindow and display it
public static void main( String args[] )
{
TimeTestWindow window = new TimeTestWindow();
window.setSize( 400, 140 );
window.setVisible( true );
}
TimeTestWindow.j
ava
Define inner class that implements
ActionListener interface Lines 77-78
Define inner class
// inner class definition for handling JTextField and
// JButton events
Line 81
Must implement method actionPerformed
private class ActionEventHandler
implements ActionListener {
of ActionListener Must implement
method
// method to handle action events
actionPerformed
public void actionPerformed( ActionEvent event )
{
// user pressed exitButton
Line 81
When user
presses JButton or Enter key,
if ( event.getSource() == exitButton
)
When user presses
System.exit( 0 );
// terminate
the actionPerformed
application
method
is invoked
// user pressed Enter key in hourField
else if ( event.getSource() == hourField ) {
time.setHour(
Integer.parseInt( event.getActionCommand() ) );
hourField.setText( "" );
}
Determine
button or key, method
actionPerformed
is invoked
Lines 84-99
actionDetermine
dependingaction
on where event depending
originated on where
event originated
// user pressed Enter key in minuteField
else if ( event.getSource() == minuteField ) {
time.setMinute(
Integer.parseInt( event.getActionCommand() ) );
minuteField.setText( "" );
}
Page 52
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// user pressed Enter key in secondField
else if ( event.getSource() == secondField ) {
time.setSecond(
Integer.parseInt( event.getActionCommand() ) );
secondField.setText( "" );
}
Outline
TimeTestWindow.j
ava
displayTime();
}
}
}
// end inner class ActionEventHandler
// end class TimeTestWindow
Page 53
Outline
TimeTestWindow.j
ava
Page 54
9.20 Inner Class Definitions (cont.)
• Anonymous inner class
– Inner class without name
– Created when class is defined in program
 2002 Prentice Hall. All rights reserved.
Page 55
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.34: TimeTestWindow.java
// Demonstrating the Time class set and get methods
// Java core packages
import java.awt.*;
import java.awt.event.*;
Outline
TimeTestWindow.j
ava
// Java extension packages
import javax.swing.*;
public class TimeTestWindow extends JFrame {
private Time time;
private JLabel hourLabel, minuteLabel, secondLabel;
private JTextField hourField, minuteField,
secondField, displayField;
// set up GUI
public TimeTestWindow()
{
super( "Inner Class Demonstration" );
// create Time object
time = new Time();
// create GUI
Container container = getContentPane();
container.setLayout( new FlowLayout() );
hourLabel = new JLabel( "Set Hour" );
hourField = new JTextField( 10 );
Page 56
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
// register hourField event handler
hourField.addActionListener(
// anonymous inner class
new ActionListener() {
Outline
Define anonymous inner class that TimeTestWindow.j
implements ActionListener ava
public void actionPerformed( ActionEvent event )
Line 36
{
Define anonymous
time.setHour(
Integer.parseInt( event.getActionCommand() ) );
inner class
hourField.setText( "" );
Inner class implements method
displayTime();
Lines
}
actionPerformed
of 38-44
}
// end anonymous inner class
); // end call to addActionListener
container.add( hourLabel );
container.add( hourField );
minuteLabel = new JLabel( "Set minute"
minuteField = new JTextField( 10 );
// register minuteField event handler
minuteField.addActionListener(
// anonymous inner class
new ActionListener() {
Inner class implements
ActionListener
method
Pass ActionListener
actionPerformed
as
argument to GUI component’s
method addActionListener
Line 33
Pass Action);
Listener to GUI
component’s method
addActionListener
Line 57-60
Repeat process for JTextField
Repeat process for
minuteField
minuteField
Page 57
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
public void actionPerformed( ActionEvent event )
{
time.setMinute(
Integer.parseInt( event.getActionCommand() ) );
minuteField.setText( "" );
displayTime();
}
Logic differs from logic in
Outline
TimeTestWindow.j
ava
Line 64-67
actionPerformed method of Logic differs from logic
} // end anonymous inner class
hourField’s inner class
in action); // end call to addActionListener
Performed method
of hourField’s inner
container.add( minuteLabel );
container.add( minuteField );
class
secondLabel = new JLabel( "Set Second" );
secondField = new JTextField( 10 );
Line 80-83
Repeat process for
Repeat process for JTextField
JTextField
secondField
secondField
secondField.addActionListener(
// anonymous inner class
new ActionListener() {
public void actionPerformed( ActionEvent event )
{
time.setSecond(
Integer.parseInt( event.getActionCommand() ) );
secondField.setText( "" );
displayTime();
}
Logic differs from logic in
}
Line 87-90
Logic differs from logic
in actionPerformed methods
of other inner classes
actionPerformed methods of
other inner classes
// end anonymous inner class
); // end call to addActionListener
Page 58
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
Outline
container.add( secondLabel );
container.add( secondField );
TimeTestWindow.j
ava
displayField = new JTextField( 30 );
displayField.setEditable( false );
container.add( displayField );
}
Line 118-131
Define anonymous
inner class that extends
WindowsAdapter to
enable closing of
JFrame
// display time in displayField
public void displayTime()
{
displayField.setText( "The time is: " + time );
}
// create TimeTestWindow, register for its window events
// and display it to begin application's execution
public static void main( String args[] )
{
TimeTestWindow window = new TimeTestWindow();
Define
// register listener for windowClosing event
window.addWindowListener(
anonymous inner class that
extends WindowsAdapter to
enable closing of JFrame
// anonymous inner class for windowClosing event
new WindowAdapter() {
// terminate application when user closes window
public void windowClosing( WindowEvent event )
{
System.exit( 0 );
}
}
// end anonymous inner class
); // end call to addWindowListener
Page 59
132
133
134
135
136
137
window.setSize( 400, 120 );
window.setVisible( true );
}
}
Outline
TimeTestWindow.j
ava
// end class TimeTestWindow
Page 60
Outline
TimeTestWindow.j
ava
Page 61
9.21 Notes on Inner Class Definitions
• Notes for inner-class definition and use
– Compiling class that contains inner class
• Results in separate .class file
• Inner classes with names have the file name
OuterClassName$InnerClassName.class
• Anonymous inner class have the file name OuterClassName$#.class where #
starts at 1 and is incremented for each additional anonymous inner class
– Inner classes with class names can be defined as
• public, protected, private or package access
– Access outer class’ this reference OuterClassName.this
– When an anonymous inner class implements an interface, the class must
define every method in the interface
• There are 7 different methods that must be defined in every class that
implements WindowListener. We only need one (windowClosing). Java
provides an adapter class that already implements all the methods of the
interface. We extend the adapter class and override just the methods we need
to change.
 2002 Prentice Hall. All rights reserved.
Page 62
9.22 Type-Wrapper Classes for Primitive
Types
• Type-wrapper class
– Each primitive type has one
• Character, Byte, Integer, Boolean, etc.
• Numeric types inherit from class Number
– Enable to represent and manipulate primitive as objects of
class Object
• Primitive data types can be processed polymorphically
– Declared as final
– Many methods are declared static
 2002 Prentice Hall. All rights reserved.
Page 63