• Post Reply Bookmark Topic Watch Topic
  • New Topic

why is there no multiple inheritance  RSS feed

 
nagarjuna borra
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi im a newbie

I have a few questions.

1.I hear a lot about the abscence of multiple inheritance in java;most of them say-it can be done using interfaces.
Although i can implement many interfaces,i still have to provide the implementation myself,but in c++ i can just inherit the code(data too) from any number of classes(assume all those classes have no conflicting method and data signatures and there's no diamond problem)

so,just becoz of the above problems java omitted the beauty of multiple inheritance???

2.are inheritances used only in libraries???,(if not why extend when i can directly make use of the library-by calling via object or class(if static))

3.are interfaces used only for dynamic method dispatch(considering im not interested in extending my class)

4.i need a practical yet simper example telling the need of oop,i.e where it gets difficult in pop and how oop made it easy

thanks in advance

Bless forums and resolvers!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java has multiple inheritance of type (or of interface). It does not have multiple inheritance of implementation.

MI of implementation primarily gives you code sharing, which is not what inheritance is about. Inheritance is primarily about type implementation and specialization, and the MI of type that you get with interfaces covers that.

Presumably the reasons the designers chose to leave out MI of implementation is that it would complicate the language (one of Java's goals is simplicity) and that the problems it would cause due to complexity and misuse outweigh the small benefits it provides.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nagarjuna borra wrote:so,just becoz of the above problems java omitted the beauty of multiple inheritance???

"Beauty" is in the eye of the beholder. Personally, I hated it when I was writing C++.

2.are inheritances used only in libraries???,(if not why extend when i can directly make use of the library-by calling via object or class(if static))

I fear you may misunderstand what inheritance is. It allows you to extend existing classes (ie, reuse their code) to create your own. If a class already exists that does what you want, why would you even be considering writing a new one?

3.are interfaces used only for dynamic method dispatch(considering im not interested in extending my class)

I don't understand the question.

4.i need a practical yet simper example telling the need of oop,i.e where it gets difficult in pop and how oop made it easy

Google is your friend. ShowSomeEffort.

Winston
 
Marcus Kelvin
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nagarjuna borra wrote:in c++ i can just [...] assume


That you are you and C++ is C++?

Multiple inheritance is just a mechanism. Can you provide a real instance where multiple inheritance represents a significant advantage over basic aggregation + use of interfaces?
 
Tim Moores
Saloon Keeper
Posts: 4035
94
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Speaking for myself, it's rare my code inherits even from a single class in Java. I prefer interfaces all the way. Designing an object hierarchy is a work of programming art, and IMO few developers manage to do it well.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
nagarjuna borra
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
3.are interfaces used only for dynamic method dispatch(considering im not interested in extending my class)

I don't understand the question.

i meant-dynamic method dispatching can be done in two ways(as far as i know)

1.a base class object can refer one of its sub class's object,when a call is made with the base class's object;the one that the base class refers is called

2.if i dont want to add any other features(extend) to my class,then still i can perform dyn meth dispatch implementing interfaces(that way i dont have to add any additional code,only i have to implement whats necessary)
I figured it in the threads

if im wrong(or stupid),please do ignore my ignorance.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nagarjuna borra wrote:i meant-dynamic method dispatching can be done in two ways(as far as i know)
1.a base class object can refer one of its sub class's object,when a call is made with the base class's object;the one that the base class refers is called

Wrong; I think you're still thinking of C++. Java instance methods are always called on the runtime class.

2.if i dont want to add any other features(extend) to my class,then still i can perform dyn meth dispatch implementing interfaces(that way i dont have to add any additional code,only i have to implement whats necessary)

Again, I don't understand your point. "What's necessary" is all the methods in the interface. Now you could implement them only as stub methods (eg, have them throw an exception perhaps) but they all still need to be implemented.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!