Java How To Program 9th Edition Deitel Solutions Manual
Java How To Program 9th Edition Deitel Solutions Manual
Programming:
Polymorphism, Solutions 10
One Ring to rule them all,
One Ring to find them,
One Ring to bring them all
and in the darkness bind them.
—John Ronald Reuel Tolkien
A philosopher of imposing
stature doesn’t think in a
vacuum. Even his most abstract
ideas are, to some extent,
conditioned by what is or is not
known in the time when he lives.
—Alfred North Whitehead
Objectives
In this chapter you’ll learn:
■ The concept of
polymorphism.
■ To use overridden methods to
effect polymorphism.
■ To distinguish between
abstract and concrete classes.
■ To declare abstract methods
to create abstract classes.
■ How polymorphism makes
systems extensible and
maintainable.
■ To determine an object’s type
at execution time.
■ To declare and implement
interfaces.
Self-Review Exercises 2
Self-Review Exercises
10.1 Fill in the blanks in each of the following statements:
a) If a class contains at least one abstract method, it’s a(n) class.
ANS: abstract.
b) Classes from which objects can be instantiated are called classes.
ANS: concrete.
c) involves using a superclass variable to invoke methods on superclass and sub-
class objects, enabling you to “program in the general.”
ANS: Polymorphism.
d) Methods that are not interface methods and that do not provide implementations must
be declared using keyword .
ANS: abstract.
e) Casting a reference stored in a superclass variable to a subclass type is called .
ANS: downcasting.
10.2 State whether each of the statements that follows is true or false. If false, explain why.
a) All methods in an abstract class must be declared as abstract methods.
ANS: False. An abstract class can include methods with implementations and abstract
methods.
b) Invoking a subclass-only method through a subclass variable is not allowed.
ANS: False. Trying to invoke a subclass-only method with a superclass variable is no al-
lowed.
c) If a superclass declares an abstract method, a subclass must implement that method.
ANS: False. Only a concrete subclass must implement the method.
d) An object of a class that implements an interface may be thought of as an object of that
interface type.
ANS: True.
Exercises
NOTE: Solutions to the programming exercises are located in the ch10solutions folder.
Each exercise has its own folder named ex10_## where ## is a two-digit number represent-
ing the exercise number. For example, exercise 10.8’s solution is located in the folder
ex10_08.
10.3 How does polymorphism enable you to program “in the general” rather than “in the spe-
cific”? Discuss the key advantages of programming “in the general.”
ANS: Polymorphism enables you to concentrate on the common operations that are ap-
plied to objects of all the classes in a hierarchy. The general processing capabilities can
be separated from any code that is specific to each class. Those general portions of the
code can accommodate new classes without modification. In some polymorphic ap-
plications, only the code that creates the objects needs to be modified to extend the
system with new classes.
10.4 What are abstract methods? Describe the circumstances in which an abstract method would
be appropriate.
ANS: An abstract method is one with keyword abstract in its declaration. Abstract meth-
ods do not provide implementations. Each concrete subclass of an abstract superclass
must provide concrete implementations of the superclass’s abstract methods. An ab-
stract method is appropriate when it does not make sense to provide an implementa-
tion for a method in a superclass (i.e., some additional subclass-specific data is
required to implement the method in a meaningful manner).
3 Chapter 10 Object-Oriented Programming: Polymorphism, Solutions