Java Reference
In-Depth Information
the return type when you are defining a polymorphic method. For polymorphic behavior, the return type of
the method in the derived class must either be the same as that of the base class method or must be of a type
that is a subclass of the return type in the base class. Where the return types are different but the return type
of the method in the derived class is a subclass of the return type in the base class, the return types are said
to be covariant . Thus the type of object returned by the derived class method is just a specialization of the
type returned by the base class method. For example, suppose that you have a method defined in a base class
Animal that has a return type of type Animal :
public class Animal {
Animal createCreature() {
// Code to create an Animal object and return a reference to it...
}
// Rest of the class definition ...
}
You can redefine the createCreature() method in a derived class Dog like this:
public class Dog extends Animal {
@Override
Dog createCreature() {
// Code to create a Dog object and return a reference to it...
}
// Rest of the class definition...
}
As long as the return type for the method in the derived class is a subclass of the return type in the base
class, as you have here, even though the return types are different you can still get polymorphic behavior. I
can summarize the conditions that need to be met if you want to use polymorphism as follows:
• The method call for a derived class object must be through a variable of a base class type.
• The method called must be defined in the derived class.
• The method called must also be declared as a member of the base class.
• The method signatures for the method in the base and derived classes must be the same.
• Either the method return type must be the same in the base and derived classes or the return types
must be covariant.
• 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 is not 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 you later see, this introduces a whole new level of capability
in programming using objects. It implies that your programs can adapt at run time to accommodate and pro-
cess different kinds of data quite automatically.
Note that polymorphism applies only 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
Search WWH ::




Custom Search