Does java support multiple interitance? Why?


Java doesnt support multiple inheritance but it provide a way through which it can enact it.
Consider the scenario is C++

Class A{

public void add(){
// some text
}
}

Class B{

public void add(){
// some text
}
}

Class C extends A,B{

public static void main(String arg[]){

C objC = new C();
objC.add(); // problem, compiler gets confused and cant
decide to call Class A or B method.
}
This problem is called Diamond problem. This problem in java is taken care with the use of interfaces In Java similar problem would look like:
interface A{
add();
}

interface B{
add();
}

class C implements A,B{

add(){
// doesnt matter which interface it belong to
}
}



Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email