Next: 2.3.1.2 Overriding.
Up: 2.3.1 General scheme
Previous: 2.3.1 General scheme
In a first attempt, the example above can be approximated in Java as
follows (changes are underlined):
2#2
Class C inherits from class A and supports the same interface
IB as class B. Furthermore, class C forwards
every call on a method in interface IB to the corresponding field
of type B.
The generalization of this translation to
``class C extends A, B1, ..., Bn {...}
'' is straightforward
given that Java supports
implementation of multiple interfaces.
We call the ``simulated superclasses'' Bi the parent
classes of C, in contrast to ``genuine superclasses'' like
A. Similarly, C is called child
class of each Bi.
How well does this translation capture multiple inheritance?
- Code Reuse:
- Class C reuses the code for classes A and
B but does require glue code to implement forwarding.
- Polymorphism:
- An instance of class C can be used for
an instance of class A
and for an instance2
of interface IB
but not for an instance of class B.
The latter is because, in general, implementing the
common interface IB does not imply
that class C supports all the methods in
class B.
If interface IB contains all the public
methods of class B and programmers use
IB instead of B for type
declarations throughout the program, then the
ability to substitute C instances for
IB instances is sufficient.
So, whether the translation supports polymorphism
depends on anticipation of future extensions (the
interface and the corresponding implements relation
in the parent class must be declared), good
documentation (of the intention that the interface
should be used for typing instead of the parent
class) and programmer discipline (you must adhere
to the documentation).
- Modification:
- Any changes to class A are automatically propagated
to class C. Similarly, adding a new field to class
B or changing a method body in class B is
transparent to class C. However, the addition
or deletion of a method from class B or changes
to a method signature in class B requires
changes to interface IB and to class C
(to install the glue code for forwarding).
- Overriding:
- Overriding and the related adaptation of
``inherited'' code from aggregated objects is
not possible. For instance, the defintion of
method b1() in C does not
influence the result of the method b().
An invocation of a() on an instance of class
C returns "C" but
an invocation of b() on an instance of class
C still returns "B".
Overriding is the core concept of inheritance [3,6]. Without
overriding, existing class hierarchies would dramatically change their
semantics and many essential concepts and techniques (for example,
protected methods, abstract classes, template methods
[17]) would not exist. Because it disables overriding, the
above translation cannot be regarded as a satisfactory approximation
of (either single or multiple) inheritance.
Next: 2.3.1.2 Overriding.
Up: 2.3.1 General scheme
Previous: 2.3.1 General scheme
T. K. Prasad