• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Multiple Inheritance in Java

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
They weren't very bright, but they were very, very big. Ad contrast:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic