Wednesday, 22 February 2017

What is Multiple Inheritance in Java ?

In Multiple Inheritance one class extends more than one class or interface. Java doesn't supports multiple inheritance because multiple inheritance result in diamond problem.
Multiple inheritance means multiple classes/ interface with same methods name and this leads to ambiguity.

What is Diamond Problem ?
In below example as we have four classes i.e A ,B ,C and D ,and classes A, B and C have method with same name and class D extends B and C (In Java class can only extends one class ,if we try to extend more than one class ,then this will leads to compile time exception),now the problem comes with class D ,to override which class method i.e. B or C.
Diamond Problem

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class A{
 
}
class B extends A {
 
}
class C extends A{
 
}
public class D extends B,C { //this leads to syntax error
 
 public static void main(String[] args) {
  
 }
}



      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



No comments:

Post a Comment