Why is multiple inheritance not allowed in java?

Showing Answers 1 - 69 of 69 Answers

pavan

  • Oct 1st, 2005
 

 pointers concept are not supported by java.and to reduce the code redundency

 

selvam_vivek

  • Oct 2nd, 2005
 

To avoid ambiguity state.  

vijay

  • Oct 6th, 2005
 

By not allowing the inheritance of multiple base classes by a single subclass, Java greatly simplifies the inheritance model. Multiple inheritance carries with it several special cases that must be handled. This adds overhead to both the compiler and the run-time system, while providing only marginal benefit for the programmer.

  Was this answer useful?  Yes

manisha divate

  • Dec 9th, 2005
 

In multiple Inheritance,

ex: class A, Class B inherited from Class A, Class C inherited from class A, Class D inherited From Class B,and Class C.

in this Class D have dual copy of the methods ,datamembers defined in Class A, Vertual Function Is there in C++ but,again the pointer are not used in java.

So to avoid this ambiguity, only single chain inheritance is there, not multiple inheritance.

mahesh

  • Jan 8th, 2006
 

to reduse the complexcity in java.

  Was this answer useful?  Yes

lhariPrasad

  • Sep 27th, 2006
 

Hi ,

Can any one solvethe diamond problem.

Assume there is a super class "Shape", Shape is extended into "Circle" and "Squre". Now I want to create a concrete class "CircleOnSqure" which extends both the Circle and Square.  How to implement this functionality in Java.

Hint : Java says Multiple inheritance can be done using Interfaces.

Regards,

Hari

  Was this answer useful?  Yes

Jetendra Ivaturi

  • Dec 7th, 2006
 

Due to the concept of overridding Java doesn't support multiple inheritance.

We have the concept of " Deadly Diamond of Death".

i.e., 

As we have an overridding concept in Java.

We know what overridding is, where the method name including parameters return types should be same.

If mutliple inheritance is provided in java.

eg:

A<- B,  A-<C  and now  B,C<-D

We have a method viz.   display() in A, we are overridding that in B and C. If D extends that two classes then if I call the display() then there will be ambiguity. To call which version of display().

As we dont have the concept of virtual functions, what we have in c++.

 

This is the reason why we dont have multiple inheritance in java.

 

 

 

  Was this answer useful?  Yes

ramesh

  • Apr 27th, 2007
 

Multiple inheritance is not allowed in java because it may lead to confusion for a programmer and this is against to java language to be a simple(one of the features of OOP's).Eg:class A extends B,C (not allowed in java)
class A extends B
class C extends A which is nothing but multiple inheritance, it means multiple inheritance can be achieved by repeated use of single inheritance.
2)we can also achieve multiple inheritance by using interfaces.

Laxminarayana V

  • May 25th, 2007
 

You are saying that if D class is having the dual copy of methods then ambiguity will come, Suppose there is least chance of having dual copy of methods,Is it ok?

  Was this answer useful?  Yes

raja

  • Jun 21st, 2007
 

In java another form of multiple inheritance is Interface.

  Was this answer useful?  Yes

Philosuffer

  • Aug 6th, 2007
 

You are saying that if D class is having the dual copy of methods then ambiguity will come, Suppose there is least chance of having dual copy of methods, Is it ok?

It may be OK, however the syntax of the language doesn't allow you to do so.

  Was this answer useful?  Yes

monica

  • Sep 7th, 2007
 

Java aims at simplicity unlike C, mutilple inheritence gives rise to errorenous results in some cases in Java hence not allowed~!

  Was this answer useful?  Yes

ramana

  • Oct 11th, 2007
 

if  class A
{
ab()
}

class B
{

ab()
}
class c extends A,B
{
}

in the above example confusion occurse for class c because we have extebding A,B.
so it will lead confusion.so java does not allowed it. through interfaces we can achieve it

  Was this answer useful?  Yes

sasikala

  • Nov 4th, 2007
 

In java the super class can extends the only one subclass.
outline line multiple inheritance
class A
{
}
class B
{
}
class C extends B A
{
}(does not support)
so java doesn't support the multiple inheritance. but it can be implemented by
using interface concept

  Was this answer useful?  Yes

sampra

  • Feb 13th, 2008
 

SUN guys has prevent to multiple inheretance  to avoid code redundancy we can achive this by using interface..  

  Was this answer useful?  Yes

Hi all,

I won't agree with Hari's Note where he mention we implement multiple inheritance by using interfaces.

Tell me hari how is it possible.

I will prove with one example.

if we write

public class Test extends Thread

then

at execution time first Object class constuctor when Thread class constructor invoked will execute,then Thread class constructor will execute when Test class constructor execute, later our class Test class constructor will execute.

then hieararchy looks like

Object<-----------Thread<-------Test

whereas if we take

public class Test implements Runnable

then Object class constructor will execute and our Test class constructor will execute and the hierarchy looks like the following

Object<-----------Test

then where is Runnable interface in the above hierarchy.

By this we can say we cann't implement multiple inheritance with the help of Interface.

I hope I am not wrong, anyway If I am wrong plz clarify me.

Rgds
Balaji

  Was this answer useful?  Yes

samjeyam

  • May 3rd, 2008
 

Found this reply by Tony Sintes (from Java World) :-

Whenever you find yourself asking why Java has or does not have some feature, consider the design goals behind the Java language. With that in mind, I started my search by skimming through "The Java Language Environment" by James Gosling and Henry McGilton (Sun Microsystems), a white paper published in May 1996 that explains some of the reasoning behind Java's design.

As the white paper states, the Java design team strove to make Java:

  • Simple, object oriented, and familiar
  • Robust and secure
  • Architecture neutral and portable
  • High performance
  • Interpreted, threaded, and dynamic


The reasons for omitting multiple inheritance from the Java language mostly stem from the "simple, object oriented, and familiar" goal. As a simple language, Java's creators wanted a language that most developers could grasp without extensive training. To that end, they worked to make the language as similar to C++ as possible (familiar) without carrying over C++'s unnecessary complexity (simple).

In the designers' opinion, multiple inheritance causes more problems and confusion than it solves. So they cut multiple inheritance from the language (just as they cut operator overloading). The designers' extensive C++ experience taught them that multiple inheritance just wasn't worth the headache.

Instead, Java's designers chose to allow multiple interface inheritance through the use of interfaces, an idea borrowed from Objective C's protocols. Multiple interface inheritance allows an object to inherit many different method signatures with the caveat that the inheriting object must implement those inherited methods. Multiple interface inheritance still allows an object to inherit methods and to behave polymorphically on those methods. The inheriting object just doesn't get an implementation free ride

Regards,
Sam

Java does not support multiple inheritance because it creates ambiguity and since Java is fully object oriented simplest OOP language it does not support this.

  Was this answer useful?  Yes

Multiple inherite is there in java but not just like c++. You can achieve it  through implementation of  multiple interface and also through creating the instance of different classes  into the class as a field member.
By doing this you achieve inheritence(acquiring the properties of that class) and you also extends the  another class
for example
 class A{
/*some methods
and variables
*/

}
class B{

/*some methods
and variables
*/
}
public interface abc{
/* declaring of methods*/
}
public interface abc1{
/* declaring of methods*/
}
1. implementing interface
Class NewClass implements abc,abc1{}
2. Through extending and creating the instance of anothetr class as member 

class NewClass extends A{
     B b;

  Was this answer useful?  Yes

s.ananth

  • Mar 7th, 2009
 

If the variables of the super-classes have the same name it is difficult for the compiler to decide which one to refer.

  Was this answer useful?  Yes

Due to the naming complexity in Base & Drived classes,

We Can't provide a solution  every time using Inheritance with same name of member,methods.

That's why It's not allowed in JAVA.

Imp: Multiple Inheritance is allowed in Java In Only the case of Interfaces.

  Was this answer useful?  Yes

pallabi87

  • May 16th, 2011
 

this is so because it leads to a problem called "dirty diamond".
that is, say class B and C inherit from class A.
class B and C might override the methods in A.
Now if D inherits from B and C(multiple inheritance), there would be a problem that which overridden method would it inherit

  Was this answer useful?  Yes

Amogh G S

  • Nov 24th, 2015
 

Consider that B and C class are inherited from class A, class B and C will be having the methods of class A. Suppose B and C class override that methods, then if class D is inherited from B or C which method it should inherit. Ambiguity exists as class D has to either inherit the original method or the method overrided. Therefore Multiple inheritance is not supported in Java. But, some part of multiple inheritance can be achiwed through interface not fully

  Was this answer useful?  Yes

Monika Srivastava

  • May 4th, 2016
 

Lets understand this through simple code

Code
  1. public class A{

  2. void test()

  3. { System.out.printl("This is a class A");

  4. }

  5.  

  6. public class B()

  7. {

  8. void test()

  9. {System.out.print("this is a class B");

  10. }

  11. }

  12.  

  13. public class C extends A,B

  14. {

  15. public static void main(String[] args)

  16. {

  17.  C ob = new C();

  18. ob.test()//Now there is an ambiguity.Compiler may not be able to understand which method to call.

  19. }

  Was this answer useful?  Yes

Varun

  • May 21st, 2017
 

Due to diamond problem. Example - class A has two subclasses class B and class C and class D implements B & C then which implementation is inherited.This causes ambiguity.

  Was this answer useful?  Yes

Pankaj Yadav

  • Jul 16th, 2017
 

In Java two class which is extended may have the same method implementation with different logic. But subclass has to choose to one of them which causes ambiguity.
To solve this Java use interface which does not have implementation.

  Was this answer useful?  Yes

Kira

  • Sep 7th, 2017
 

Because sub class goes into the state of ambiguity if diffrent super classes implement same method name or same variable name.

  Was this answer useful?  Yes

pranit

  • Oct 12th, 2017
 

Java does not supports multiple inheritance at all, but it
supports similar feature interface. Which removes the
ambiguity problem (Deadly Diamonds Of Death) and complexity
of multiple inheritance. Although interface is not the part
of inheritance because inheritance is achieved in java
through extends keyword only. Interface is the part of
aggregation not inheritance.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions