Jump to content

  • Log In with Google      Sign In   
  • Create Account


Java and single inheritance


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
252 replies to this topic

#1 zacharya   Members   -  Reputation: 127

Like
Likes
Like

Posted 16 February 2004 - 02:13 AM

I just want to know why Java doesn''t support multiple inheritance.... Everybody told me that Java''s OOP concept is better than C++ but what makes me curious is why in Java I can''t do multiple inheritance like in C++.

Sponsor:

#2 MasterTnT   Members   -  Reputation: 122

Like
Likes
Like

Posted 16 February 2004 - 02:18 AM

It is impossible to have multiple inheritance in JAVA with concrete class (only with interface).

#3 Arild Fines   Members   -  Reputation: 968

Like
Likes
Like

Posted 16 February 2004 - 02:21 AM

Because it''s very hard to implement, and almost impossible to implement correctly(just look at what a mess it is in C++. People say Eiffel got it right, though).

--
AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.
[Project site] [Blog] [RSS] [Browse the source] [IRC channel]

#4 Oluseyi   Staff Emeritus   -  Reputation: 1678

Like
Likes
Like

Posted 16 February 2004 - 02:25 AM

quote:
Original post by zacharya
Everybody told me that Java''s OOP concept is better than C++ but what makes me curious is why in Java I can''t do multiple inheritance like in C++.
Choice by the language designers. Specifically, a reaction to a common problem in C++.

In C++, multiple inheritance can often lead to the "diamond inheritance" problem - where a class derived from two classes with a common ancestor consequently receives two (or more) copies of the ancestral class. This is solved with virtual inheritance, but the Java language designers thought it better to attempt to idiot-proof the language by disallowing MI and substituting multiple implementation of interfaces (which can have no data members) instead.

P.S. This lack of respect for my intelligence - repeated often in Java - is one of the reasons I dislike it. Caveat emptor.

#5 Oluseyi   Staff Emeritus   -  Reputation: 1678

Like
Likes
Like

Posted 16 February 2004 - 02:29 AM

quote:
Original post by Arild Fines
Because it''s very hard to implement, and almost impossible to implement correctly(just look at what a mess it is in C++. People say Eiffel got it right, though).
I''d like to know your opinions on MI in Python. I find the order of inclusion method interesting, though I''ll readily admit that something about Python makes inheritance less of a critical method - probably the fact that its dynamic typing system and introspective qualities allow for fairly generic programming.

#6 DrPizza   Members   -  Reputation: 160

Like
Likes
Like

Posted 16 February 2004 - 03:24 AM

quote:
Because it''s very hard to implement, and almost impossible to implement correctly(just look at what a mess it is in C++. People say Eiffel got it right, though).

Eiffelists say that. It''s not clear that they''re right, however. MI does raise certain issues, but it''s probably worth having in spite of these issues.


#7 dmikesell   Members   -  Reputation: 157

Like
Likes
Like

Posted 16 February 2004 - 06:33 AM

quote:
Original post by DrPizza
quote:
Because it''s very hard to implement, and almost impossible to implement correctly(just look at what a mess it is in C++. People say Eiffel got it right, though).

Eiffelists say that. It''s not clear that they''re right, however. MI does raise certain issues, but it''s probably worth having in spite of these issues.



Yes, it''s worth having because then I have the choice to use it if I want to. Just like operator overloading, pointers, etc. When in the right hands, they are useful tools. When in the wrong hands, they can be dangerous, but those hands are what languages like Java were invented for.



--
Dave Mikesell Software

#8 Stonicus   Members   -  Reputation: 157

Like
Likes
Like

Posted 16 February 2004 - 10:01 AM

I''ve never understood the problem people have with multiple inheritance. It is one of the most powerful features in C++ and I don''t think I could program without it. Maybe some little fluffy flowery programs, but I try to avoid those kinds of projects. Sure, you have to keep up with your virtual inheritances, but it isn''t that hard a concept to use. I think people make it harder than it needs to be, cuz it isn''t that hard and can allow you some EXTREMELY powerful code.


#9 Aldacron   GDNet+   -  Reputation: 3101

Like
Likes
Like

Posted 16 February 2004 - 02:11 PM

quote:
Original post by Oluseyi
P.S. This lack of respect for my intelligence - repeated often in Java - is one of the reasons I dislike it. Caveat emptor.


That's an interesting viewpoint. I find it to be a useful separation of interface and implementation. Consider the difference between an interface and an abstract class in Java. In C++, they are one and the same - we create them using pure virtual methods. However, often C++ interfaces have concrete methods implemented. This, IMO, can be a poor design decision in some cases and is often done by those who don't understand the difference. I don't consider Java's version an insult to anyone's intelligence any more than a sophisticated IDE is (when compared to command line development). It was a design decision to aid in reducing common mistakes.

Personally, the only need I have ever for multiple inheritance in C++ is to implement interface-like functionality. I'm sure others may have had a need for inheriting multiple concrete objects, but I have yet to run into such a case myself. At least, not since I learned to think in terms of interfaces. I think there are many cases when people think they need to inherit multiple concrete objects when they really don't.

[edited by - aldacron on February 16, 2004 9:12:33 PM]

#10 Oluseyi   Staff Emeritus   -  Reputation: 1678

Like
Likes
Like

Posted 16 February 2004 - 02:34 PM

quote:
Original post by Aldacron
That''s an interesting viewpoint. I find it to be a useful separation of interface and implementation. Consider the difference between an interface and an abstract class in Java. In C++, they are one and the same - we create them using pure virtual methods. However, often C++ interfaces have concrete methods implemented. This, IMO, can be a poor design decision in some cases and is often done by those who don''t understand the difference. I don''t consider Java''s version an insult to anyone''s intelligence any more than a sophisticated IDE is (when compared to command line development). It was a design decision to aid in reducing common mistakes.
An IDE still allows you to drop to command line; the option remains, allowing you to make the choice. IDEs extend the command line, as opposed to hiding it. The analogy is tenuous.

quote:
Personally, the only need I have ever for multiple inheritance in C++ is to implement interface-like functionality. I''m sure others may have had a need for inheriting multiple concrete objects, but I have yet to run into such a case myself (emphasis added).
I need say no more.

By the way, I dislike C++ about half as much as I dislike Java.

#11 SabreMan   Members   -  Reputation: 504

Like
Likes
Like

Posted 17 February 2004 - 01:38 AM

quote:
Original post by Oluseyi
Choice by the language designers. Specifically, a reaction to a common problem in C++.

That''s the brochureware rationalisation, but there''s also the conspiracy theory that it was a choice by the marketing department, who would have been fully aware having a product to market is more lucrative than waiting for the engineers to realise an MI approach which isn''t so `broken''. It might have helped if they hadn''t based their object model on one perceived to be broken...


#12 SabreMan   Members   -  Reputation: 504

Like
Likes
Like

Posted 17 February 2004 - 01:45 AM

quote:
Original post by Aldacron
Personally, the only need I have ever for multiple inheritance in C++ is to implement interface-like functionality.

How about this one: without MI of implementation, contractual assertions on interfaces have to be specified in each and every concrete subclass, rather than just once in the interface itself.


#13 TangentZ   Members   -  Reputation: 372

Like
Likes
Like

Posted 17 February 2004 - 02:02 AM

quote:
Original post by Aldacron
Personally, the only need I have ever for multiple inheritance in C++ is to implement interface-like functionality. I''m sure others may have had a need for inheriting multiple concrete objects, but I have yet to run into such a case myself. At least, not since I learned to think in terms of interfaces. I think there are many cases when people think they need to inherit multiple concrete objects when they really don''t.



This same tired "argument" comes equally often from the C#
"fans".

Just because you didn''t find any use, it doesn''t mean other
people haven''t.

The option to use implementation-inheritance should have
been up to the programmer, not the language designer.



Kami no Itte ga ore ni zettai naru!

#14 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 17 February 2004 - 08:46 AM

I agree with the point about the only ''real use'' for multiple inheritance is with multiple interface inheritance not implementation.

Sure you can come up with examples of multiple implementation inheritance, but almost invariably they make for really horrible code. And equally invariably they can be written much cleaner and simpler via an interface viewpoint.

Oh and the arguement about C++ multiple implementation inheritance being ''great because I can use it if I want it'', is bogus on two counts. One already described, but the other is that it cripples the language when you do want to do interface style programming.

For example...

struct IBlah
{
virtual void A()=0;
};

struct IBlah2ublic IBlah
{
virtual void B()=0;
};

class CAbstractBlobublic IBlah
{
....
void A() { /* Do something */ }
}

class CBlobublic CAbstractBlob,public IBlah2
{
...
}

CBlob is great isn''t it? As I have to tell C++ I want IBlah2''s A to call CAbstractBlobs implementation. Jeez...

You might think thats a crap example - but it really isn''t. If you have done any COM programming then you hit this kind of thing all the time.

So in a nut shell I believe Java + C# single inhertiance architecture is absolutely the way to go. It loses no flexibility (you can still implement everything you can with C++ multiple inheritance - as you have multiple interface inheritance) whilst simplifying the whole notion of an objects. On top of that it removes the madness described above.

Sign me up.

#15 SabreMan   Members   -  Reputation: 504

Like
Likes
Like

Posted 17 February 2004 - 09:13 AM

quote:
Original post by Anonymous Poster
I agree with the point about the only ''real use'' for multiple inheritance is with multiple interface inheritance not implementation.

I''ve already presented an effective counter-example to this assertion.
quote:

Sure you can come up with examples of multiple implementation inheritance, but almost invariably they make for really horrible code. And equally invariably they can be written much cleaner and simpler via an interface viewpoint.

That does not hold for the scenario I described.
quote:

Oh and the arguement about C++ multiple implementation inheritance being ''great because I can use it if I want it'', is bogus on two counts. One already described, but the other is that it cripples the language when you do want to do interface style programming.

No it doesn''t. What cripples the language is the conflation of packaging mechanism and class construct. This problem still exists in Java, only mitigated by the lack of MI of implementation. This approach does not address the problem, merely ignores it in order to achieve ease of implementation (of the Java platform).
quote:

So in a nut shell I believe Java + C# single inhertiance architecture is absolutely the way to go.

And I believe the way to go is to not derive new languages using something badly designed as the baseline. Java does not attempt to answer any interesting problems of CS, it exists to make money for its stakeholders. I take the removal of multiple implementation inheritance to be evidence in support of that assertion. Given that Guy Steele was involved early on in the Java project, I find it hard to believe there was no awareness of possible solutions to the MI problem.


#16 Zahlman   Moderators   -  Reputation: 1682

Like
Likes
Like

Posted 17 February 2004 - 09:20 AM

I can''t think of a time I''ve ever wanted multiple inheritance.

Hell, I don''t even want *single* inheritance all that often, and tend not to build deep hierarchies. I tend to do more with delegation (are there any languages out there that facilitate delegation of multiple methods, say by doing simple delegation to aggregates by default?).

If you really feel you need proper MI, then use a language which supports it. And STFU.

#17 Trap   Members   -  Reputation: 684

Like
Likes
Like

Posted 17 February 2004 - 09:50 AM

If you dissallow people to do certain things you have to provide a better alternative.

Nameclashes can easily be resolved through renaming as in Eiffel. Implementation inheritance has a special construct in Eiffel, expanded (non-conforming) inheritance.

Seperating interface and implementation is a double edged sword. If there is a reasonable default implementation you end up with redundant code in every class implementing the interface.

Back to learning lisp...

#18 gph-gw   Members   -  Reputation: 122

Like
Likes
Like

Posted 17 February 2004 - 09:56 AM

quote:
Original post by Stonicus
Sure, you have to keep up with your virtual inheritances, but it isn''t that hard a concept to use.



I''m sure you can. But what about the poor soul who has to maintain your code?

#19 dmikesell   Members   -  Reputation: 157

Like
Likes
Like

Posted 17 February 2004 - 10:08 AM

C++ was designed to be a multi-purpose language used for a variety of problem domains. There may not be many valid uses for MI, but for the problems where it is the best solution it''s invaluable. Same with operator overloading, templates, pointers, etc.

The problem I have with Java is that they take away all of these things and yet the advocates still try to claim that it''s a multi-purpose language. It''s not. It''s a language for client server web applications.

--
Dave Mikesell Software

#20 Aldacron   GDNet+   -  Reputation: 3101

Like
Likes
Like

Posted 17 February 2004 - 01:01 PM

quote:
Original post by tangentz
quote:
Original post by Aldacron
Personally, the only need I have ever for multiple inheritance in C++ is to implement interface-like functionality. I''m sure others may have had a need for inheriting multiple concrete objects, but I have yet to run into such a case myself. At least, not since I learned to think in terms of interfaces. I think there are many cases when people think they need to inherit multiple concrete objects when they really don''t.



This same tired "argument" comes equally often from the C#
"fans".

Just because you didn''t find any use, it doesn''t mean other
people haven''t.



I wasn''t arguing anything. Nor was I saying other people haven''t had a use for it. Notice the part that reads "I''m sure other people may have...". I was really only looking for a specific example from Oluseyi on when MI is really needed.

quote:

The option to use implementation-inheritance should have
been up to the programmer, not the language designer.



I do have an issue with this oft repeated mantra, though. Language designers are free to design their language in anyway they see fit. The important thing is that we, as programmers, have choices. We can pick the language best suited for the job. There''s no such thing as a one-size-fits-all language. Each project has its own requirements for performance, portability, time to market, extensibility, yadda, yadda. Then there are other factors such as the experience of the team and available funds. All of this should play a role in which language to use for a particular project. Blindly choosing one over the other for every project will often lead to a failure to meet one or more of the project requirements.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS