my dog learned polymorphism*
The moose likes Java in General and the fly likes Multiple Inheritance in Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Multiple Inheritance in Java" Watch "Multiple Inheritance in Java" New topic
Author

Multiple Inheritance in Java

Ramya Krishnaswamy
Greenhorn

Joined: Mar 18, 2006
Posts: 1
I have a doubt. Why is Multiple inheritance been removed from Java [for any specific reasons]? And how would an Interface replace multiple inheritance.


With kind Regards<br />Ramya
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi,

Welcome to JavaRanch!

Multiple inheritance has not been "removed" from Java; Java has never had multiple inheritance. It was never added in the first place. Why was it not included? Because in C++, in particular, it causes so many headaches. Java is designed to be a simple language to use and understand, and multiple inheritance as implemented in a strongly-typed language generally is not.

A Java class can have many different runtime types by implementing many different interfaces. This is one half of the "multiple inheritance equation" --- the one that's important from a design perspective. If you don't understand why this is more important than the other half -- the "inheriting actual code" part -- then you will learn with some experience.

But in any case, a Java class can inherit many types by implementing interfaces. To reuse code from multiple classes, though, you have to use delegation -- i.e., keep a member of another class, and forward method calls to it, rather than trying to extend it.


[Jess in Action][AskingGoodQuestions]
vignesh hariharan
Ranch Hand

Joined: Jun 23, 2005
Posts: 77
let me give u an example to illustrate..

suppose u have 3 classes.. two super classes classA and classB..
and both has a method which r named as show().. let both the classes have same method name but different defnition..

if suppose u extend both to ur classC which is ur subclass.. then if u create a an object of ur classC obj=new classC(); and say obj.show(); which method will be called?? will it call from classA or classB?? assuming that u r not overriding that show() in ur subclass..it will throw an error.. but java allows u to create same method names in two or more different classes..so there lies the difficulty or the bug which is there in c++.. so if u implement as an interface u r supposed to override.. it becomes mandatory.. then u will obviously call the subclass version.

thus multiple inheritance is not introduced in java.. and this is one of the plus point of java as well..
[ March 20, 2006: Message edited by: vignesh hariharan ]

Regards,
vignesh
Roy Simon
Ranch Hand

Joined: Aug 26, 2005
Posts: 62
hi

Multiple inheritance when used tends to complicate ur code a lot and also the fact that all graphs (obtained by multiple inhertance) can be represented by trees (simple or hierarchical inheritance or multilevel inheritance) by implementing a proper design. hence first of all multiple inheritance is not required as a matter of speaking and if u really need it that badly its not totally removed from java. by using interfaces u can get runtime inheritance but the difference being that u dont have a super implementation of the interface methods

Regards
Simon
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple Inheritance in Java
 
Similar Threads
Multiple Inheritance - Reusability
java does not support multiple inheritance. Is it a limitation to java?
Interface
why interface
Interface Inhertance