? 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 1
Chapter 14
Inheritance and Polymorphism
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 2
Chapter 14 Objectives
After you have read and studied this chapter,you
should be able to
Write programs that are easily extensible and
modifiable by applying polymorphism in
program design.
Define reusable classes based on inheritance
and abstract classes and abstract methods.
Define methods using the protected modifier.
Parse strings using a StringTokenizer object.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 3
Defining Classes with Inheritance
We introduced the concept of inheritance in Chapter 1.
We will present a concrete example of using an
inheritance in Java to define related classes.
Suppose we want to model graduate and
undergraduate students in maintaining a class roster.
For both types of students we keep their name,three
test scores,and the final course grade,The formula for
deriving the final course grades are different for
undergraduate and graduate students.
How shall we implement the two types of students?
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 4
Student with Two Subclasses
GraduateStudent
computeCourseGrade
UndergraduateStudent
computeCourseGrade
Student
Student
setTestScore
getTestScore
NUM_OF_TESTS
3
courseGrade
name
test[ ]…
We will define three classes,
Student
GraduateStudent
UndergraduateStudent
Implementation of
computeCourseGrade is
unique to each subclass.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 5
Using Polymorphism Effectively
Polymorphism is a feature that allows the
programmer to send the same message to objects
from different classes.
Polymorphism allows the same variable to refer to
objects from different (but related) classes.
Student student;
student = new GraduateStudent();
student.computeCourseGrade( );
Student student;
student = new UndergraduateStudent();
student.computeCourseGrade( );
This will call the
method of
GraduateStudent
This will call the
method of
UndergraduateStudent
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 6
Creating the roster Array
Student roster[ ] = new Student[40]; roster is an array of
Student objects,
Create instances of
the subclasses of
Student.
roster[0] = new GraduateStudent( );
roster[1] = new UndergraduateStudent( );
roster[2] = new UndergraduateStudent( );
roster[3] = new GraduateStudent( );
0 1 2 3 4 36 37 38 39roster
Graduate-
Student
Under-
graduate-
Student
Under-
graduate-
Student
Graduate-
Student
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 7
Processing the roster Array
for (int i = 0; I < numberOfStudents; i++ ) {
roster[i].computeCourseGrade( );
}
Use instanceOf to
determine the class
the object belongs to.
int undergradCount = 0;
for (int i = 0; i < numberOfStudents; i++ )
{
if ( roster[i] instanceOf
UndergraduateStudent ) {
undergradCount++;
}
}
Notice how the
polymorphism is used
here.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 8
Inheritance and Member Accessibility
Which data members and methods of a superclass are
accessible from the methods of its subclasses?
There is a third modifier called protected in addition to public
and private.
public
private
protected
Superclass
Subclass
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 9
Access from the Subclass Methods
Only the private elements of the superclass are not
accessible from its subclasses,All other types of
elements are accessible.
mySub
Subclass
Superclass
inaccessible
accessible
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 10
Access from the Outside
Only the public elements are accessible from the outside objects.
mySub
Subclass
Superclass
mySuper
Superclass
Client
test
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 11
Access from Another Instance
All elements of an object are accessible from other
instances of the same class.
anInstance
AClass
anotherInstance
AClass
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 12
Inheritance and Constructors
Constructors of a superclass are not inherited by its subclasses.
Since constructors are not inherited,we must define one for a class (or use the default constructor).
If the constructor we define for a class does not include the explicit call to the superclass constructor,then the compiler
adds one for us.
public MyClass
{
private int var;
public MyClass( )
{
super( );
var = 10;
}
.,,
}
public MyClass
{
private int var;
public MyClass( )
{
var = 10;
}
.,,
}
becomes
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 13
Abstract Superclasses and Methods
Often we want to place elements common to all subclasses in their superclass.
But we do not want any instances to be created from the superclass.
In such case,we designate the superclass as an abstract class.
It is common for an abstract class to include an abstract method that must be implemented by the
descendant classes.
If a class includes an abstract method,then it is considered as an abstract class and must have the
abstract modifier in the class declaration.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 14
Sample Abstract Class
Abstract method has
no method body.
abstract class Student
{
.,,
abstract public void computeCourseGrade( );
.,,
}
Reserved word
abstract in the class
declaration.
class GraduateStudent extends Student
{
.,,
public void computeCourseGrade( )
{
//method body comes here
}
.,,
}
Abstract method is
must be fully
implemented in the
descendant class.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 15
IS-A and HAS-A
To model an IS-A relationship,use the inheritance,
GraduateStudent IS-A Student,Truck IS-A Vehicle,
and so forth.
To model a HAS-A relationship,use the composition
(an object is composed of other objects),Vehicle
HAS-A Body,Tire,and Engine.
Both IS-A and HAS-A,when used correctly,will
improve the degree of code reuse.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 16
Sample Program,Computing Course Grades
Problem Statement
Write an application that reads in a textfile organized in the manner
shown below and displays the final course grades,The course grades
are computed differently for the undergraduate and graduate
students based on the formulas listed on page 658,The input textfile
format is as follows:
A single line is used for information on one student.
Each line uses the format
<Type> <Name> <Test 1> <Test 2> <Test 3>
where <Type> designates either a graduate or undergraduate student,
<Name> designates the student’s first and last name,and <Test i>
designates the ith test score.
End of input is designated by the word END,The case of the letters is
insignificant.
Capabilities
Read data from a textfile.
Compute course grades for undergraduate and graduate students
using different formula.
Display the formatted result.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 17
Development Steps
1,Start with the program skeleton,Define the ComputeGradesMain and skeleton ComputeGrades
classes.
2,Implement the printResult method,Define any other methods necessary to implement printResult.
3,Implement the computeGrade method,Define any other methods necessary to implement computeGrade.
4,Implement the readData method,Define any other methods necessary to implement readData.
5,Finalize and look for improvements.
The End
Chapter 14
Inheritance and Polymorphism
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 2
Chapter 14 Objectives
After you have read and studied this chapter,you
should be able to
Write programs that are easily extensible and
modifiable by applying polymorphism in
program design.
Define reusable classes based on inheritance
and abstract classes and abstract methods.
Define methods using the protected modifier.
Parse strings using a StringTokenizer object.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 3
Defining Classes with Inheritance
We introduced the concept of inheritance in Chapter 1.
We will present a concrete example of using an
inheritance in Java to define related classes.
Suppose we want to model graduate and
undergraduate students in maintaining a class roster.
For both types of students we keep their name,three
test scores,and the final course grade,The formula for
deriving the final course grades are different for
undergraduate and graduate students.
How shall we implement the two types of students?
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 4
Student with Two Subclasses
GraduateStudent
computeCourseGrade
UndergraduateStudent
computeCourseGrade
Student
Student
setTestScore
getTestScore
NUM_OF_TESTS
3
courseGrade
name
test[ ]…
We will define three classes,
Student
GraduateStudent
UndergraduateStudent
Implementation of
computeCourseGrade is
unique to each subclass.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 5
Using Polymorphism Effectively
Polymorphism is a feature that allows the
programmer to send the same message to objects
from different classes.
Polymorphism allows the same variable to refer to
objects from different (but related) classes.
Student student;
student = new GraduateStudent();
student.computeCourseGrade( );
Student student;
student = new UndergraduateStudent();
student.computeCourseGrade( );
This will call the
method of
GraduateStudent
This will call the
method of
UndergraduateStudent
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 6
Creating the roster Array
Student roster[ ] = new Student[40]; roster is an array of
Student objects,
Create instances of
the subclasses of
Student.
roster[0] = new GraduateStudent( );
roster[1] = new UndergraduateStudent( );
roster[2] = new UndergraduateStudent( );
roster[3] = new GraduateStudent( );
0 1 2 3 4 36 37 38 39roster
Graduate-
Student
Under-
graduate-
Student
Under-
graduate-
Student
Graduate-
Student
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 7
Processing the roster Array
for (int i = 0; I < numberOfStudents; i++ ) {
roster[i].computeCourseGrade( );
}
Use instanceOf to
determine the class
the object belongs to.
int undergradCount = 0;
for (int i = 0; i < numberOfStudents; i++ )
{
if ( roster[i] instanceOf
UndergraduateStudent ) {
undergradCount++;
}
}
Notice how the
polymorphism is used
here.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 8
Inheritance and Member Accessibility
Which data members and methods of a superclass are
accessible from the methods of its subclasses?
There is a third modifier called protected in addition to public
and private.
public
private
protected
Superclass
Subclass
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 9
Access from the Subclass Methods
Only the private elements of the superclass are not
accessible from its subclasses,All other types of
elements are accessible.
mySub
Subclass
Superclass
inaccessible
accessible
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 10
Access from the Outside
Only the public elements are accessible from the outside objects.
mySub
Subclass
Superclass
mySuper
Superclass
Client
test
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 11
Access from Another Instance
All elements of an object are accessible from other
instances of the same class.
anInstance
AClass
anotherInstance
AClass
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 12
Inheritance and Constructors
Constructors of a superclass are not inherited by its subclasses.
Since constructors are not inherited,we must define one for a class (or use the default constructor).
If the constructor we define for a class does not include the explicit call to the superclass constructor,then the compiler
adds one for us.
public MyClass
{
private int var;
public MyClass( )
{
super( );
var = 10;
}
.,,
}
public MyClass
{
private int var;
public MyClass( )
{
var = 10;
}
.,,
}
becomes
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 13
Abstract Superclasses and Methods
Often we want to place elements common to all subclasses in their superclass.
But we do not want any instances to be created from the superclass.
In such case,we designate the superclass as an abstract class.
It is common for an abstract class to include an abstract method that must be implemented by the
descendant classes.
If a class includes an abstract method,then it is considered as an abstract class and must have the
abstract modifier in the class declaration.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 14
Sample Abstract Class
Abstract method has
no method body.
abstract class Student
{
.,,
abstract public void computeCourseGrade( );
.,,
}
Reserved word
abstract in the class
declaration.
class GraduateStudent extends Student
{
.,,
public void computeCourseGrade( )
{
//method body comes here
}
.,,
}
Abstract method is
must be fully
implemented in the
descendant class.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 15
IS-A and HAS-A
To model an IS-A relationship,use the inheritance,
GraduateStudent IS-A Student,Truck IS-A Vehicle,
and so forth.
To model a HAS-A relationship,use the composition
(an object is composed of other objects),Vehicle
HAS-A Body,Tire,and Engine.
Both IS-A and HAS-A,when used correctly,will
improve the degree of code reuse.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 16
Sample Program,Computing Course Grades
Problem Statement
Write an application that reads in a textfile organized in the manner
shown below and displays the final course grades,The course grades
are computed differently for the undergraduate and graduate
students based on the formulas listed on page 658,The input textfile
format is as follows:
A single line is used for information on one student.
Each line uses the format
<Type> <Name> <Test 1> <Test 2> <Test 3>
where <Type> designates either a graduate or undergraduate student,
<Name> designates the student’s first and last name,and <Test i>
designates the ith test score.
End of input is designated by the word END,The case of the letters is
insignificant.
Capabilities
Read data from a textfile.
Compute course grades for undergraduate and graduate students
using different formula.
Display the formatted result.
2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 14 - 17
Development Steps
1,Start with the program skeleton,Define the ComputeGradesMain and skeleton ComputeGrades
classes.
2,Implement the printResult method,Define any other methods necessary to implement printResult.
3,Implement the computeGrade method,Define any other methods necessary to implement computeGrade.
4,Implement the readData method,Define any other methods necessary to implement readData.
5,Finalize and look for improvements.
The End