aspose file tools*
The moose likes Beginning Java and the fly likes inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "inheritance" Watch "inheritance" New topic
Author

inheritance

aditya kumar
Greenhorn

Joined: Apr 02, 2008
Posts: 12
Java suppors multiple inheritance,but can anybody justify that it only supported single inheritance.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

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.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

A = Class
B = Class
C = Interface



does this code justify?


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
Originally posted by Muhammad Saifuddin:
A = Class
B = Class
C = Interface



does this code justify?
No
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

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

Joined: Oct 02, 2003
Posts: 11480
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
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

    Joined: Apr 20, 2006
    Posts: 3355

    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

    Joined: Dec 06, 2005
    Posts: 1321

    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

    Joined: Apr 20, 2006
    Posts: 3355

    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

    Joined: Dec 06, 2005
    Posts: 1321

    Hi Raghavan,

    We all learn through experience.


    indeed, Javaranch is a place.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: inheritance