Java Reference
In-Depth Information
be assigning an mp3 player object to a CDPlayer variable. That, most
likely, doesn't make sense and would cause problems if it was allowed
to happen.
SR 10.6
When a child class overrides the definition of a parent's method, two
versions of that method exist. If a polymorphic reference is used to
invoke the method, the version of the method that is invoked is deter-
mined by the type of the object being referred to, not by the type of the
reference variable.
SR 10.7
The StaffMember class is abstract because it is not intended to be instan-
tiated. It serves as a placeholder in the inheritance hierarchy to help
organize and manage the objects polymorphically.
SR 10.8
The pay method has no meaning at the StaffMember level, so is declared
as abstract. But by declaring it there we guarantee that every object of
its children will have a pay method. This allows us to create an array of
StaffMember objects, which is actually filled with various types of staff
members, and pay each one. The details of being paid are determined
by each class as appropriate.
SR 10.9
It depends. The pay method invocation is polymorphic. The actual
method that is invoked is determined at run time and is based on the
class of the object referenced by the current (according to the value of
count ) element of the staff list.
10.3 Polymorphism via Interfaces
SR 10.10
An interface name can be used as the type of a reference. Such a refer-
ence variable can refer to any object of any class that implements that
interface. Because all classes implement the same interface, they have
methods with common signatures, which can be dynamically bound.
SR 10.11
a. illegal - Speaker is an interface and interfaces do not have constructors.
b. legal - the Dog class implements Speaker.
c. legal - note that all the classes involved implement Speaker.
d. legal - Philosopher implements Speaker , so it is legal to assign
a philosopher object to a speaker variable.
e. illegal - first is declared to be a Speaker , so we cannot invoke
the Philosopher method pontificate through first.
10.4 Sorting
SR 10.12
The Comparable interface contains a single method called compareTo ,
which should return an integer that is less than zero, equal to zero,
Search WWH ::




Custom Search