Why does java doesnot support multiple inheritance?

Answers were Sorted based on User's Feedback



Why does java doesnot support multiple inheritance?..

Answer / rathnakar

--> Java follows Super Class Object and Sub Class reference
at runtime to excute inherited and overridden methods.

--> Java doesnot support multiple inheritence by using
classes.becoz java compiler gets ambiguity if one method
is defined in many child classes. this happens due to
above reason

--> so execution fails . but it can be achieved using
interfaces. becoz it method over riding and every child
class is identified uniquely w.rt to interface.

Is This Answer Correct ?    8 Yes 0 No

Why does java doesnot support multiple inheritance?..

Answer / balu

multiple inheritance is not available in java for the following reasons
1.it leads to confusion for a java program
2.the programmer can achieve multiple inheritance by using interfaces
3.and also we can achieve it by repeatdly using single inheritance.

Is This Answer Correct ?    7 Yes 0 No

Why does java doesnot support multiple inheritance?..

Answer / msraju

Every class in java must have only one root class (i.e
Object class) directly or indirectly.
If Class C extends Class A and Class B(this approach is not
supported in java ),here Class A will one root Object
class,similarly Class B will have Another root class.i.e
Here Class C is having two root classes....For this reason
java not supporting Multiple inheritance with respective
Objacts.
Every objects need some common functionalities which are
available in Object class.

Is This Answer Correct ?    2 Yes 0 No

Why does java doesnot support multiple inheritance?..

Answer / deep

Multiple inheritance Oops i think diamond problem..


class A {
public void x() {
System.out.println("Hello");
}

class B extends A {
public void x() {
System.out.println("Hello");
}

class C extends A {
public void x() {
System.out.println("Hello");
}

// Main class
class AB extends B,C // Remember this is not possible in java, a class can extend only one class in java..
{
public static void main(String args[]) {

AB a = new AB();
a.x(); // compiler gets confused whom should i call.. which methods should i invoke since both methods has same name i.e public void x(), complier get scared and raise an error of ambiguity
}
}

To avoid this type of situation, designers of java decided that a class can extends only one class so there would be no ambiguity of methods.. and to over come this issue they added interface..

class can implements n number of interface but can extend only one class.. a interface can extends n number of interface..

Variables declared inside interface are by default,, static and final.. and methods are abstract i.e unimplemented methods, that doesn't have body.. methods of interface are by default abstract and public..

you can make interface public or depends on your requirement.. if you want a interface to access outside of another package better make it public cause non public classes are confined within same package..

Is This Answer Correct ?    1 Yes 0 No

Why does java doesnot support multiple inheritance?..

Answer / rana.ankur

Suppose consider a method funX() which is in class Z.

Suppose a programmer A inherited the class Z to class X and
overrided the funX().So this class will have the new
implementation of funX(). ie class X extend Z
Suppose a programmer D inherited the class Z to class Y and
overrided the funX().So this class will have the new
implementation of funX(). ie class Y extend Z


If Multiple Inheritance is permitted in java, then if the
new programmer C inherited both the classes and he didn't
done any overriding of method funX() then if he calls the
funX() ,the JVM will not know which method to call i.e.,
either the method in class X or method in class Y.

Because of this inconsistencies,Multiple inheritance is not
permitted in java

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More Core Java Interview Questions

What technique is carried out to find out if a particular string is empty?

0 Answers  


Difference between static binding and dynamic binding?

0 Answers  


what is difference between prepare stetement and callable starement with example?

1 Answers   CMC,


Is void a data type in java?

0 Answers  


Why lambda expression is used in java?

0 Answers  






what is multithreading?

5 Answers   Virtusa,


How can we make sure main() is the last thread to finish in java program?

0 Answers  


Does .length start 0 java?

0 Answers  


What is the purpose of using javap?

0 Answers  


1) Find the Merge point of two linked lists. 2) Swap two bits of integer. 3) Reverse individual words of the sentence. 4) Reverse string without using temp string.

2 Answers   HCL,


What are the differences between graph and tree?

0 Answers   Amazon,


Can we create object of static class?

0 Answers  


Categories