|
Multiple Inheritance
Multiple inheritance is a scenario where a child class is inherited from more than one parent classes. For e.g., class C extends class A as well as class B.
Java doesn't directly support multiple inheritance. This means that if you have two classes A & B then you can not write a code like follows:
classs C extends A, B {....}
Above kind of line would result in compile time error. Though at first it might seem a limitation, but it is not; but first lets understand why this kind of restriction has been put.
The answer lies in runtime polymorphism. Consider following scenario in detail:
class A {
void disp(){System.out.println("Inside A");}
}
class B {
void disp(){System.out.println("Inside B");}
}
If C was to be allowed to extend both A & B simultaneously as follows:
class C extends A, B{
void display(){super.disp();}
}
then during runtime, Java won't be able to decide whether to call A's disp() or B's disp().
There are two options for this - mostly this kind of requirement can be easily handled with the help of using multilevel inheritance; i.e. re-designing your class hierarchy in linear fashion; second option is to use interfaces for runtime polymorphism.
|