Java Reference
In-Depth Information
class C extends B {
int k;
void doEvenMore () {
doSomething ();
doSomethingMore ();
k = i+j;
}
}
Here the doEvenMore() method internally calls the doSomething() method
from class A and the doSomethingMore() method from class B .Aninstance
of class C can use the class C data and methods and also those of both classes A
and B .
Inheritance does more than just reduce the size of the class definitions. We see
shortly that the inheritance mechanism offers several new capabilities including
the ability to redefine, or override ,amethod in the superclass with a new one.
(The terms superclass , base class , and parent class all mean the same thing and
are used interchangeably, as are the terms subclass and child class .)
Class inheritance in Java is strictly linear. A subclass may extend only one
direct superclass, though all of that parent's superclasses get inherited as well in
a chaining fashion, as shown in the class C example above. Unlike C
,Java
does not permit multiple class inheritance, which is inheriting from more than
one direct parent class. That is, given two classes X and Y ,itisnot possible in
Java to create a class Z that extends both X and Y .
++
There are times that multiple class inheritance could be useful, but it was inten-
tionally omitted by the Java designers because correctly implementing and using
multiple class inheritance is fraught with difficulty. Java interfaces, to be discussed
later, do permit multiple inheritance, providing many of the benefits of multiple
class inheritance without the drawbacks.
Search WWH ::




Custom Search