• 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

inheritance

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java suppors multiple inheritance,but can anybody justify that it only supported single inheritance.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by nalayak kumar:
Java suppors multiple inheritance,but can anybody justify that it only supported single inheritance.



Simple! Try extending more than one class at a time.
 
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A = Class
B = Class
C = Interface



does this code justify?
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Muhammad Saifuddin:
A = Class
B = Class
C = Interface



does this code justify?

No
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess Muhammed has not understood the OP's intention or the question. He has asked can anybody justify that Java supports only single inheritance!
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the simplest answer for why java doesn't support multiple inheritance is that the people who designed the language felt that having it caused more problems than it solved.

As I understand it, the idea was (in part) to remove the complicated, esoteric stuff that caused LOTS of headaches from C/C++, and keep the 'good' stuff. Who's definition of good? That would be those designers.

Whether they were right or wrong can be debated, and will be for a long time.
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Multiple inheritance means that a class can obtain working functionality from more than one source. Since only classes and enums can embody functionality, it would mean a class can inherit directly from several classes.
This is called diamond inheritance, because you can draw diagrams like thisThere is a very similar diagram in B Meyer, Object-Oriented Software Construction 2/e (I think 1998).

The problem is that there might be methods with the same name and different functionality in the different branches. Eiffel (Meyer's language) insists one of the methods be renamed; obviously when Java was developed Gosling et al thought this was too complicated an prohibited multiple inheritance. When Microsoft developed C# a few years later, they also only permitted single inheritance.

There are several ways to mimic multiple inherita
  • Multiple interfaces. Since interfaces are empty, they don't inherit any functionality, and the collision problem is greatly reduced.
  • Inner classes.
  • "Favour Composition over Inheritance." By using fields which are objects of a superclass you can get one inheritance hierarchy in the class and another in the fields, which gives most of the benefits of multiple inheritance.
  • Does that help?
    [ April 09, 2008: Message edited by: Campbell Ritchie ]
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    That's great information shared by EFH and Campbell.

    By the way, the scenario what Campbell depicted is called as Diamond Problem. In C++, it is resolved by using Virtual base class.

    I was aware of the problem but was not knowing its name or terminology. So, had to ask for more clues in an interview!
     
    Muhammad Saifuddin
    Ranch Hand
    Posts: 1325
    Android Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Raghavan Muthu:
    I guess Muhammed has not understood the OP's intention or the question.



    Thanks Radhvan for this post, to give a chance to make my point clear regarding the question and OP's intention in java.

    as per my understanding of the question, the original poster is alerady know about single inheritence, so I posted the alternative way to apply multiple inhertance in Java (See my last post).

    As we know Java programming language does not premit multiple inheritance, but interfaces provide an alternative.

    hope this post clear my point and answered the question.
     
    Raghavan Muthu
    Ranch Hand
    Posts: 3389
    Mac MySQL Database Tomcat Server
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Originally posted by Muhammad Saifuddin:

    Thanks Radhvan for this post, to give a chance to make my point clear regarding the question and OP's intention in java.



    Radhvan?? My pleasure Muhammed. At times it happens but no issues. We all learn through experience.


    as per my understanding of the question, the original poster is alerady know about single inheritence, so I posted the alternative way to apply multiple inhertance in Java (See my last post).



    Yes it looked so. But he wanted to have a proof for the contradictory.
     
    Muhammad Saifuddin
    Ranch Hand
    Posts: 1325
    Android Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Raghavan,

    We all learn through experience.



    indeed, Javaranch is a place.
     
    Getting married means "We're in love, so let's tell the police!" - and invite this tiny ad to the wedding:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic