SeatWork2 1
SeatWork2 1
SeatWork2 1
it is a name.
#&353"/%3644&--
This chapter covers inheritance, polymorphism, and interfaces, three key concepts
in object-oriented programming. These concepts are also needed in order to
use many of the libraries that come with the Java programming language.
Polymorphism makes objects behave as you expect them to and allows you to
focus on the specifications of those behaviors. Inheritance will enable you to use
an existing class to define new classes, making it easier to reuse software. Finally,
interfaces allow you to specify the methods that a class must implement.
OBJECTIVES
After studying this chapter, you should be able to
t %FTDSJCFQPMZNPSQIJTNBOEJOIFSJUBODFJOHFOFSBM
t %FGJOFJOUFSGBDFTUPTQFDJGZNFUIPET
t %FTDSJCFEZOBNJDCJOEJOH
t %FGJOFBOEVTFEFSJWFEDMBTTFTJO+BWB
t 6OEFSTUBOEUIFSPMFPGJOIFSJUBODFUPQSPEVDFXJOEPXJOHJOUFSGBDFTXJUIJO
Java application programs
PREREQUISITES
You need to have read the material in Chapters 1 through 6 before you can
understand the material in this chapter. Chapter 7 is needed to understand
some of the examples presented in Sections 8.3 and 8.4.
Suppose we define a class for vehicles that has instance variables to record the
vehicles number of wheels and maximum number of occupants. The class
also has accessor and mutator methods. Imagine that we then define a class
for automobiles that has instance variables and methods just like the ones
576
8.1 Inheritance Basics 577
in the class of vehicles. In addition, our automobile class would have added
instance variables for such things as the amount of fuel in the fuel tank and
the license plate number and would also have some added methods. Instead
of repeating the definitions of the instance variables and methods of the class
of vehicles within the class of automobiles, we could use Javas inheritance
mechanism, and let the automobile class inherit all the instance variables and
methods of the class for vehicles.
Inheritance allows you to define a very general class and then later define Inheritance
more specialized classes that add some new details to the existing general class lets you define
definition. This saves work, because the more specialized class inherits all the specialized classes
from a general
properties of the general class and you, the programmer, need only program
one
the new features.
Before we construct an example of inheritance within Java, we first need
to set the stage. Well do so by defining a simple class called Person. This
DMBTTTIPXO JO -JTUJOH JT TP TJNQMF UIBU UIF POMZ BUUSJCVUF JU HJWFT B
public Person()
{
name = "No name yet";
}
public Person(String initialName)
{
name = initialName;
}
public void setName(String newName)
{
name = newName;
}
public String getName()
{
return name;
}
public void writeOutput()
{
System.out.println("Name: " + name);
}
public boolean hasSameName(Person otherPerson)
{
return this.name.equalsIgnoreCase(otherPerson.name);
}
}
578 CHAPTER 8 / Inheritance, Polymorphism, and Interfaces
person is a name. We will not have much use for the class Person by itself, but
we will use it when defining other classes.
Most of the methods for the class Person are straightforward. For example,
the method hasSameName is similar to the equals methods weve seen, but
note that it considers uppercase and lowercase versions of a letter to be the
same when comparing names.
Derived Classes
Suppose we are designing a college record-keeping program that has records
for students, faculty, and other staff. There is a natural hierarchy for grouping
these record types: They are all records of people. Students are one subclass
of people. Another subclass is employees, which includes both faculty and
staff. Students divide into two smaller subclasses: undergraduate students and
graduate students. These subclasses may further subdivide into still smaller
subclasses.
Figure 8.1 depicts a part of this hierarchical arrangement. Although your
program may not need any class corresponding to people or employees,
thinking in terms of such classes can be useful. For example, all people have
names, and the methods of initializing, displaying, and changing a name will
be the same for student, staff, and faculty records. In Java, you can define a
class called Person that includes instance variables for the properties that
belong to all subclasses of people. The class definition can also contain all the
Person
Student Employee
methods that manipulate the instance variables for the class Person. In fact,
we have already defined such a PersonDMBTTJO-JTUJOH
-JTUJOH DPOUBJOT UIF EFGJOJUJPO PG B DMBTT GPS TUVEFOUT " TUVEFOU JT B
person, so we define the class Student to be a derived class, or subclass, of
the class Person. A derived class is a class defined by adding instance variables
and methods to an existing class. We say that the derived class extends the
existing class. The existing class that the derived class is built upon is called
A derived class
the base class, or superclass. In our example, Person is the base class and extends a base
Student is the derived class. We indicated this in the definition of Student class and inherits
JO-JTUJOHCZJODMVEJOHUIFQISBTFextends Person on the first line of the the base classs
class definition, so that the class definition of Student begins public members
(continued)
580 CHAPTER 8 / Inheritance, Polymorphism, and Interfaces
The class Studentlike any other derived classis said to inherit the
public instance variables and public methods of the base class that it extends.
When you define a derived class, you give only the added instance variables
and the added methods. Even though the class Student has all the public
instance variables and all the public methods of the class Person, we do not
declare or define them in the definition of Student. For example, every object
of the class Student has the method getName, but we do not define getName
in the definition of the class Student.
A derived class, such as Student, can also add some instance variables
or methods to those it inherits from its base class. For example, Student
defines the instance variable studentNumber and the methods reset,
getStudentNumber, setStudentNumber, writeOutput, and equals, as well as
some constructors. (We will postpone the discussion of constructors until we
finish explaining the other parts of these class definitions.)
Notice that although Student does not inherit the private instance
variable name from Person, it does inherit the method setName and all the
other public methods of the base class. Thus, Student has indirect access to
name and so has no need to define its own version. If s is a new object of the
class Student, defined as
Student s = new Student();
we could write
s.setName("Warren Peace");
Screen Output
Name: Warren Peace
Student Number: 1234
When discussing derived classes, it is common to use terminology derived A base class is
from family relationships. A base class is often called a parent class A derived also called a
class is then called a child class. This makes the language of inheritance very superclass, a
smooth. For example, we can say that a child class inherits public instance parent class, and
an ancestor class
variables and public methods from its parent class.
This analogy is often carried one step further. A class that is a parent of
a parent of a parent of another class (or some other number of parent of
iterations) is often called an ancestor class. If class A is an ancestor of class B,
then class B is often called a descendant of class A.
class inherits all of the public methods and public instance variables from
the base class and can add more instance variables and methods.
SYNTAX
EXAMPLE:
A method
overrides another RECAP Overriding Method Definitions
if both have
the same name,
In a derived class, if you include a method definition that has the same
return type, and
parameter list name, the exact same number and types of parameters, and the same
return type as a method already in the base class, this new definition
(continued)
8.1 Inheritance Basics 583
replaces the old definition of the method when objects of the derived
class receive a call to the method.
When overriding a method definition, you cannot change the return type
of the method. Since the signature of a method does not include the
return type, you can say that when one method overrides another, both
methods must have the same signature and return type.
In this case, the class Student would have two methods named getName: It
would inherit the method getName, with no parameters, from the base class
Person -JTUJOH
BOE JU XPVME BMTP IBWF UIF NFUIPE OBNFE getName,
with one parameter, that we just defined. This is because the two getName
methods have different numbers of parameters, and thus the methods use
overloading.
If you get overloading and overriding confused, remember this:
Overloading places an additional load on a method name by using it for
another method, whereas overriding replaces a methods definition.
When a method is declared as final, the compiler knows more about how
it will be used, and so the compiler can generate more efficient code for the
method.
584 CHAPTER 8 / Inheritance, Polymorphism, and Interfaces
A final class An entire class can be declared final, in which case you cannot use it as a
cannot be a base base class to derive any other class. You are not very likely to need the final
class modifier right now, but you will see it in the specifications of some methods
in the standard Java libraries.
Since the instance variable name is a private instance variable in the definition
of the class Person, it cannot be directly accessed by name within the
definition of the class Student. Thus, the definition of the method reset in
the class Student is
public void reset(String newName, int newStudentNumber)
{
setName(newName); Valid definition
studentNumber = newStudentNumber;
}
Private instance
variables in a It cannot be as follows:
base class are
not inherited by public void reset(String newName, int newStudentNumber)
a derived class; {
they cannot name = newName;//ILLEGAL! Illegal definition
be referenced studentNumber = newStudentNumber;
directly by name }
within a derived
class
As the comment indicates, this assignment will not work, because a derived
class does not inherit private instance variables from its base class. Thus, the
definition of reset in the class Student uses the method setName to set the
name instance variable.
8.1 Inheritance Basics 585
The fact that a private instance variable of a base class cannot be accessed
by name within the definition of a method of a derived class often seems
wrong to people. After all, students should be able to change their own
names, rather than being told Sorry, name is a private instance variable of
the class Person. If you are a student, you are also a person. In Java, this is
also true; an object of the class Student is also an object of the class Person.
However, the rules regarding the use of private instance variables must be as
weve described, or else the private designation would be pointless. If private
instance variables of a class were accessible in method definitions of a derived
class, whenever you wanted to access a private instance variable, you could
simply create a derived class and access it in a method of that class. This
would mean that all private instance variables would be accessible to anybody
who wanted to put in a little extra effort.
Similarly, private methods in a base class are not directly accessible by Private methods
name within any other class, not even a derived class. The private methods still in a base class are
exist, however. If a derived class calls an inherited public method that contains not inherited by a
derived class; they
an invocation of a private method, that invocation still works. However, a
cannot be called
derived class cannot define a method that invokes a private method of the directly by name
base class. This should not be a problem. Private methods should serve only from a derived
as helping methods, and so their use should be limited to the class in which class
they are defined. If you want a method to serve as a helping method in a
number of derived classes, it is more than just a helping method, and you
should make the method public.
Person
An Employee is a
Person and so forth
hence the arrows point up.
Student Employee
IBOE5IFPOMZTJHOJGJDBOUEJGGFSFODFCFUXFFOUIFOPUBUJPOJO'JHVSFBOE
UIBU JO 'JHVSF JT UIBU UIF MJOFT JOEJDBUJOH JOIFSJUBODF JO 'JHVSF IBWF
unfilled arrowheads. Note that the arrowheads point up from the derived
class to the base class. These arrows show the is-a relationship. For example,
a Student is a Person. In Java terms, an object of type Student is also of type
Person.
The arrows also help in locating method definitions. If you are looking
for a method definition for some class, the arrows show the path you (or the
computer) should follow. If you are looking for the definition of a method
used by an object of the class Undergraduate, you first look in the definition
of the class Undergraduate; if it is not there, you look in the definition of
Student; if it is not there, you look in the definition of the class Person.
Figure 8.3 shows more details of the inheritance hierarchy for two classes:
Person and one of its derived classes, Student. Suppose s references an object
Person
name: String
Student
studentNumber: int
of the class Student. The diagram in Figure 8.3 tells you that definition of the
method getStudentNumber in the call
int num = s.getStudentNumber();
is found within the class Student, but that the definition of setName in
s.setName("Joe Student");
5. Can a derived class directly invoke a private method of the base class?