GeekInterview.com
Series: Subject: Topic:
Question: 35 of 37

why is multiple inheritance not allowed in java?

Asked by: Interview Candidate | Asked on: Sep 30th, 2005
Showing Answers 1 - 23 of 23 Answers
pavan

Answered On : Oct 1st, 2005

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

Yes  2 Users have rated as useful.
  
Login to rate this answer.
selvam_vivek

Answered On : Oct 2nd, 2005

To avoid ambiguity state.  

Yes  2 Users have rated as useful.
  
Login to rate this answer.
vijay

Answered On : 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.

  
Login to rate this answer.
manisha divate

Answered On : 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.

Yes  2 Users have rated as useful.
  
Login to rate this answer.
mahesh

Answered On : Jan 8th, 2006

To reduse the complexcity in java.

  
Login to rate this answer.
lhariPrasad

Answered On : Sep 27th, 2006

View all answers by lhariPrasad

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

  
Login to rate this answer.
Jetendra Ivaturi

Answered On : 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<-DWe 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.    

  
Login to rate this answer.
ramesh

Answered On : 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).

Yes  1 User has rated as useful.
  
Login to rate this answer.
Laxminarayana V

Answered On : 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?

  
Login to rate this answer.
raja

Answered On : Jun 21st, 2007

In java another form of multiple inheritance is Interface.

  
Login to rate this answer.
Philosuffer

Answered On : 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.

  
Login to rate this answer.
monica

Answered On : Sep 7th, 2007

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

  
Login to rate this answer.
ramana

Answered On : 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

  
Login to rate this answer.
sasikala

Answered On : Nov 4th, 2007

In java the super class can extends the only one subclass.outline line multiple inheritanceclass 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

  
Login to rate this answer.
sekarbala

Answered On : Dec 24th, 2007

View all answers by sekarbala

Multiple inheritance is possible in java by using the interfaces.

  
Login to rate this answer.
sampra

Answered On : Feb 13th, 2008

View all answers by sampra

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

  
Login to rate this answer.

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 writepublic class Test extends Threadthen 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 likeObject<-----------Thread<-------Testwhereas if we takepublic class Test implements Runnablethen Object class constructor will execute and our Test class constructor will execute and the hierarchy looks like the followingObject<-----------Testthen 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.RgdsBalaji

  
Login to rate this answer.
samjeyam

Answered On : May 3rd, 2008

View all answers by samjeyam

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 familiarRobust and secureArchitecture neutral and portableHigh performanceInterpreted, threaded, and dynamicThe 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 rideRegards,Sam

Yes  2 Users have rated as useful.
  
Login to rate this answer.
upen_singh121

Answered On : Sep 20th, 2008

View all answers by upen_singh121

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.

  
Login to rate this answer.
Tarun Wadhwa

Answered On : Sep 22nd, 2008

View all answers by Tarun Wadhwa

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 classfor example class A{/*some methodsand variables*/}class B{/*some methodsand variables*/}public interface abc{/* declaring of methods*/}public interface abc1{/* declaring of methods*/}1. implementing interfaceClass NewClass implements abc,abc1{}2. Through extending and creating the instance of anothetr class as member class NewClass extends A{     B b;} 

  
Login to rate this answer.
s.ananth

Answered On : Mar 7th, 2009

View all answers by s.ananth

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

  
Login to rate this answer.
mundravijay

Answered On : Dec 7th, 2009

View all answers by mundravijay

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.

  
Login to rate this answer.
pallabi87

Answered On : May 16th, 2011

View all answers by pallabi87

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

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.