Java Reference
In-Depth Information
First of all, polymorphism works with derived class objects. It also depends on a new capability that is
possible within a class hierarchy that we haven't met before. Up to now, we have always been using a
variable of a given type to reference objects of the same type. Derived classes introduce some new
flexibility in this. Of course, we can store a reference to a derived class object in a variable of the
derived class type, but we can also store it in a variable of any direct or indirect base class type. More
than that, a reference to a derived class object
must
be stored in a variable of a direct or indirect class
type for polymorphism to work. For instance, as shown in the previous diagram, a variable of type
Dog
can be used to store a reference to an object of any type derived from
Dog
. If the
Dog
class was derived
from the
Animal
class here, a variable of type
Animal
could also be used to reference
Spaniel
,
Chihuahua
, or
Collie
objects.
Polymorphism means that the actual type of the object involved in a method call determines which
method is called, rather than the type of the variable being used to store the reference to the object. In
the previous diagram, if
aDog
contains a reference to a
Spaniel
object, the
bark()
method for that
object will be called. If it contains a reference to a
Collie
object, the
bark()
method in the
Collie
class will be called. To get polymorphic operation when calling a method, the method must be a
member of the base class - the class type of the variable you are using - as well that of the class type of
the object involved. So in our example, the
Dog
class must contain a
bark()
method, as must each of
the derived classes. You cannot call a method for a derived class object using a variable of a base class
type if the method is not a member of the base class. Any definition of the method in a derived class
must have the same signature and the same return type as in the base class, and must have an access
specifier that is no more restrictive. Indeed, if you define a method in a derived class with the same
signature as a method in a base class, any attempt to specify a different return type, or a more restrictive
access specifier, will be flagged as an error by the compiler.
The conditions that need to be met if you want to use polymorphism can be summarized as:
The method call for a derived class object must be through a variable of a base class type.
❑
The method called must also be a member of the base class.
❑
The method signature must be the same in the base and derived classes.
❑
The method return type must be the same in the base and derived classes.
❑
The method access specifier must be no more restrictive in the derived class than in the base.
❑
When you call a method using a variable of a base class type, polymorphism results in the method that
is called being selected based on the type of the object stored, not the type of the variable. Because a
variable of a base type can store a reference to an object of any derived type, the kind of object stored
will not be known until the program executes. Thus the choice of which method to execute has to be
made dynamically when the program is running - it cannot be determined when the program is
compiled. The
bark()
method that is called through the variable of type
Dog
in the earlier illustration,
may do different things depending on what kind of object the variable references. As we will see, this
introduces a whole new level of capability in programming using objects. It implies that your programs
can adapt at runtime to accommodate and process different kinds of data quite automatically.
Note that polymorphism only applies to methods. It does not apply to data members. When you access
a data member of a class object, the variable type always determines the class to which the data member
belongs. This implies that a variable of type
Dog
can only be used to access data members of the
Dog
class. Even when it references an object of type
Spaniel
, for instance, you can only use it to access
data members of the
Dog
part of a
Spaniel
object.