9
Object-Oriented
Programming: Inheritance
Objectives
In this chapter you’ll:
Understand inheritance and
another class.
Use access modifier
protected in a superclass to
give subclass methods access
to these superclass members.
2Chapter 9 Object-Oriented Programming: Inheritance
Self-Review Exercises
9.1 Fill in the blanks in each of the following statements:
a) is a form of software reusability in which new classes acquire the members of
existing classes and embellish those classes with new capabilities.
b) A superclass’s members can be accessed in the superclass declaration and in
subclass declarations.
c) In a(n) relationship, an object of a subclass can also be treated as an object of
its superclass.
d) In a(n) relationship, a class object has references to objects of other classes as
members.
e) In single inheritance, a class exists in a(n) relationship with its subclasses.
f) A superclass’s members are accessible anywhere that the program has a refer-
ence to an object of that superclass or to an object of one of its subclasses.
g) When an object of a subclass is instantiated, a superclass is called implicitly
or explicitly.
h) Subclass constructors can call superclass constructors via the keyword.
9.2 State whether each of the following is true or false. If a statement is false, explain why.
a) Superclass constructors are not inherited by subclasses.
b) A has-a relationship is implemented via inheritance.
c) A Car class has an is-a relationship with the SteeringWheel and Brakes classes.
d) When a subclass redefines a superclass method by using the same signature, the subclass
is said to overload that superclass method.
Exercises
NOTE: Solutions to the programming exercises are located in the ch09solutions folder.
Each exercise has its own folder named ex09_## where ## is a two-digit number representing
the exercise number. For example, exercise 9.3’s solution is located in the folder ex09_03.
9.4 (Software Reuse) Discuss the ways in which inheritance promotes software reuse, saves time
during program development and helps prevent errors.
Exercises 3
9.5 (Student Inheritance Hierarchy) Draw an inheritance hierarchy for students at a university
similar to the hierarchy shown in Fig. 9.2. Use Student as the superclass of the hierarchy, then ex-
9.6 (Shape Inheritance Hierarchy) The world of shapes is much richer than the shapes included
in the inheritance hierarchy of Fig. 9.3. Write down all the shapes you can think of—both two-di-
9.7 (protected vs. private) Some programmers prefer not to use protected access, because
9.9 (What Does Each Code Snippet Do?)
a) Assume the following method call is in an overridden earnings method in a subclass:
9.10 (Write a Line of Code) Write a line of code that performs each of the following tasks:
a) Specify that class PieceWorker inherits from class Employee.
9.9 What Does Each Code Snippet Do?
a) Assume that the following method call is located in an overridden earnings method in
a subclass:
super.earnings()
b) Assume that the following line of code appears before a method declaration:
@Override
4Chapter 9 Object-Oriented Programming: Inheritance
c) Assume that the following line of code appears as the first statement in a constructor’s
body:
super(firstArgument, secondArgument);
9.10 (Write a Line of Code) Write a line of code that performs each of the following tasks:
b) Call superclass Employee’s toString method from subclass PieceWorker’s toString
method.
c) Call superclass Employee’s constructor from subclass PieceWorker’s constructor—as
sume that the superclass constructor receives three Strings representing the first name,
last name and social security number.
9.11 (Using super in a Constructor’s Body) Explain why you would use super in the first state-
ment of a subclass constructor’s body.
9.12 (Using super in an Instance Method’s Body) Explain why you would use super in the body
of a subclass’s instance method.
9.13 (Calling get Methods in a Class’s Body) In Figs. 9.10–9.11 methods earnings and to-
String each call various get methods within the same class. Explain the benefits of calling these get
methods within the classes.