• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Multiple inheritance

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ,
the reason why we dont have multiple inheritance in java is it will defeat the basic logic -- Java is designed to be simple and Object Oriented. ofcourse multiple inheritance makes the code complex . but will it not be a pure oo concept . i m not sure how that could be . or i m wrong in my very basic understanding of multiple inheritance & java


expecting the answers
thanks in advance
ashok :roll:
 
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Strictly speaking Java has Single Inheritance of implementation and multiple inheritance of interfaces.

Java designers wanted to avoid the possibility of the dreaded Diamond of Death.

Retaining multiple inheritance of interfaces allowed Java to leverage two advantages of MI: Interface( classe)s and ease of (polymorphic) Use. Many have lamented the lacking capability for "Combining Modules/Libraries" which is used for Mixins. It seems that in the Java domain this niche is being filled by Aspect-Oriented Programming (e.g. aspectj; Writing Mixins using AspectJ) - it has its own challenges.
 
ashok ganesan
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi thanks peer . ya i could understand that Diamond Problem has its own complexities . but there could be a chance when one class has to extend two other classes and use the two super class methods (functionalities) and it will add its own . i just want to know how this is achieved by multiple interface inheritance.i could not the simulate the above situation with multiple interface inheritance.



-- ashoK
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is typically solved in Java by using composition instead. In my experience, most often this results in a better, more flexible design than MI would.

As an aside, "pure OO" doesn't have any meaningful definition I'm aware of.
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also say Java has multiple inheritance of "types."
 
Peer Reynders
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ashok ganesan:
but there could be a chance when one class has to extend two other classes and use the two super class methods (functionalities) and it will add its own . i just want to know how this is achieved by multiple interface inheritance.



Originally posted by Ilja Preuss:
This is typically solved in Java by using composition instead.



Ilja, is correct - C++/COM circles use a specific term for this kind of composition: Object Containment and Delegation

So given that you have an interface Z that is already implemented by class A and class B, you want to create class C that implements interface Z in terms of A and B. An instance of C creates its own instances of A and B (or has them injected from the outside) and calls their functions as necessary during its own implementation of Z. So C contains A and B and delegates to them when appropriate. In this particular scenario we inherit from a single interface but we have multiple implementations that are weaved together by the developer. Using Containment/Delegation you can always fake (multiple) implementation inheritance as long as you have multiple interface inheritance.

Interestingly even the C++ community which has multiple implementation inheritance available prefers Containment/Delegation over non-polymorphic inheritance (i.e. protected and private inheritance).
Uses and Abuses of Inheritance, Part 1
Uses and Abuses of Inheritance, Part 2
[ May 22, 2007: Message edited by: Peer Reynders ]
 
I once met a man from Nantucket. He had a tiny ad
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic