Why java does not supports multiple inheritance?
Answers were Sorted based on User's Feedback
Inheritance:
The concept of getting properties of one class object to another class object is known as inheritance.
Here properties means variable and methods.
Types of Inheritance:
Multiple inheritance.
Multilevel inheritance.
Multiple inheritance:
The concept of Getting the properties from multiple class objects to sub class object with same priorities is known as multiple inheritance.
Java Doesn't Support multiple Inheritance.
Diamond problem:
In multiple inheritance there is every chance of multiple properties of multiple objects with the same name available to the sub class object with same priorities leads for the ambiguity.
//Multiple inheritance program
Class A{
}
Class B extends A{
public void show(){
}
}
Class C extends A{
public void show(){
}
}
Class D extends B,C{ // not supported by java leads to syntax error.
}
Why multiple inheritanc eis not possible in java
We have two classes B and c which are inheriting A class properties.
Here Class D inheriting B class and C class So properties present in those classes will be available in java.
more http://www.instanceofjava.com/2014/12/why-java-does-not-supports-multiple.html
Is This Answer Correct ? | 15 Yes | 0 No |
Answer / mahendra kola
Java Does not support multiple inheritance because java is a simple programming language, but here may be we can get ambiguity
The process of getting variable and methods from more then one class to one or more number of sub classes is called as multiple inheritances.
if we write java program like bellow we get ambiguity problem ....
class A
{
public int n=10;
public void m1(){
System.out.println("A class Method");
}
}
class B
{
int n=20;
public void m1(){
System.out.println("B class Method");
}
}
class C extends A,B
{
System.out.println(n);
public static void main(String[] args){
System.out.println("Child Class");
}
}
• Incase of multiple inheritance, if declare the same variables with deferent values and same methods with different with implementation at two super classes then if we access that variables and methods at the respective subclass then which super class variable will be accessed and which super class methods will be accessed is a confusion state.
• Java is a simple programming language, it should not allowed confusion oriented features, due to this reason, java has not allowed multiple inheritances.
Is This Answer Correct ? | 1 Yes | 0 No |
Answer / shree
Multiple inheritance eliminated from java because it leads to confusion in many of cases & causes ambiguity.
So to avoid that complexity java does not support multiple inheritance.
/Multiple inheritance program
Class A
{
public void show(){.....}
}
Class B extends A
{
public void show(){.....}
}
Class C extends A{
public void show(){......}
}
Class D extends B,C{ // not supported by java leads to syntax error.
}
Here class D will get confuse which show() method to execute as there are 2 show() methods are available inherited from class B & C.
but to provide functionality of inheritance java provides new concept called INTERFACE.
Is This Answer Correct ? | 0 Yes | 0 No |
What does 3 dots mean in java?
What is method and methodology?
What is java basic concept?
What is the use of default method in interface in java? Explain
What is the importance of static variable?
What is thread life cycle in java?
Can we force garbage collector to run ?
Same common question what is Map,Set,HashMap,List????
What data type is string java?
class A { public void disp(int a,int b) { System.out.println("hai"); } } class B { public void disp(int a,int b,int c) { System.out.println("hai"); } } above program is overloading or overriding?
What is array command?
Can a class declared as private be accessed outside it’s package?