Java Reference
In-Depth Information
All reference types are polymorphic in Java. This is also known as dynamic
dispatch or late binding (or sometimes dynamic binding ).
A derived class is type-compatible with its base class, meaning that a
reference variable of the base class type may reference an object of the
derived class, but not vice versa. Sibling classes (that is, classes derived from
a common class) are not type-compatible.
4.1.4 inheritance hierarchies
As mentioned earlier, the use of inheritance typically generates a hierarchy of
classes. Figure 4.6 illustrates a possible Person hierarchy. Notice that Faculty
is indirectly, rather than directly, derived from Person
If X IS-A Y , then X
is a subclass of Y
and Y is a super-
class of X . These
relationships are
transitive.
so faculty are people
too! This fact is transparent to the user of the classes because IS-A relation-
ships are transitive. In other words, if X IS-A Y and Y IS-A Z , then X IS-A Z .
The Person hierarchy illustrates the typical design issues of factoring out com-
monalities into base classes and then specializing in the derived classes. In
this hierarchy, we say that the derived class is a subclass of the base class and
the base class is a superclass of the derived class. These relationships are tran-
sitive, and furthermore, the instanceof operator works with subclasses. Thus
if obj is of type Undergrad (and not null ), then obj instanceof Person is true .
4.1.5 visibility rules
We know that any member that is declared with private visibility is accessible
only to methods of the class. Thus as we have seen, any private members in
the base class are not accessible to the derived class.
figure 4.6
The Person hierarchy
Person
Student
Employee
Undergrad
Graduate
Faculty
Staff
 
Search WWH ::




Custom Search