Wednesday 19 February 2014

Multiple Inheritance

In multiple inheritance one class derived from more than one super class.

Java doesn't support multiple inheritance.

Why Java not supporting multiple inheritance ?
Lets say there is a method called print(), in the super classes A and B
class A{
 void print(){
  System.out.println(I am in A);
 }
}

class B{
 void print(){
  System.out.println(I am in B);
 }
}

class C extends A, B{
 public static void main(String args[]){
  print();
 }
}

Class “C” is calling the method print(), then Java run time has two options, one is in the class A and other is in class B, here is an ambiguity. Of course there are languages like C++ provides a way to solve this problem, but to make the Java language simple, Java doesn't support multiple inheritance directly.

Java supports multiple inheritance using interfaces. A Java class allowed to implement more than one interface.


Hierarchical Inheritance                                                 Hybrid Inheritance                                                 Home

No comments:

Post a Comment