• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inheritance

 
aditya kumar
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java suppors multiple inheritance,but can anybody justify that it only supported single inheritance.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Muhammad Saifuddin
Ranch Hand
Posts: 1324
Android Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A = Class
B = Class
C = Interface



does this code justify?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • 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: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • 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!
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12015
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • 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: 3381
    Mac MySQL Database Tomcat Server
    • 0
    • Mark post as helpful
    • send pies
    • 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: 1324
    Android Java Windows
    • 0
    • Mark post as helpful
    • send pies
    • 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: 3381
    Mac MySQL Database Tomcat Server
    • 0
    • Mark post as helpful
    • send pies
    • 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: 1324
    Android Java Windows
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Raghavan,

    We all learn through experience.


    indeed, Javaranch is a place.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic