Java Reference
In-Depth Information
Figure 10.4
Actor hierarchy with
Drawable class
Drawable
Actor
Animal
Hunter
Rabbit
Fox
Ant
10.5.4
Drawable actors: multiple inheritance
The scenario presented here uses a structure known as multiple inheritance. Multiple
inheritance exists in cases where one class has more than one immediate superclass. 4 The
subclass then has all the features of both superclasses and those defined in the subclass
itself.
Concept:
Multiple inherit-
ance. A situation in
which a class inher-
its from more than
one superclass is
called multiple in-
heritance.
Multiple inheritance is quite easy to understand in principle but can lead to significant
complications in the implementation of a programming language. Different object-
oriented languages vary in their treatment of multiple inheritance: some languages allow
the inheritance of multiple superclasses; others do not. Java lies somewhere in the middle.
It does not allow multiple inheritance of classes but provides another construct, called an
“interface,” that allows a limited form of multiple inheritance. Interfaces are discussed in
the next section.
10.6
Interfaces
Up to this point in the topic, we have used the term “interface” in an informal sense, to repre-
sent that part of a class that couples it to other classes. Java captures this concept more formally
by allowing interface types to be defined.
At first glance, interfaces are similar to classes, with the most obvious difference being that
none of their method definitions includes a method body. Thus, they are similar to abstract
classes in which all methods are abstract.
4 Don't confuse this case with the regular situation where a single class might have several superclasses
in its inheritance hierarchy, such as Fox , Animal , Actor , and Object . This is not what is meant by
multiple inheritance.
 
 
Search WWH ::




Custom Search