• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple Inheritance in Java

 
Ramya Krishnaswamy
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a doubt. Why is Multiple inheritance been removed from Java [for any specific reasons]? And how would an Interface replace multiple inheritance.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
vignesh hariharan
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Roy Simon
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic