Jump to content
  • Advertisement
Sign in to follow this  
Lode

Multiple Inheritance

This topic is 3918 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

C++ has multiple inheritance. Appearantly, many people consider multiple inheritance bad however. Java for example has interfaces but you can inherit only from one class. My question is about what you can do with real multiple inheritance, where each class you inherit from can have an implementation. For instance, say you have a class B and C that inherit from A, and another class D. That structure must remain intact. Now you want to give B and D a new parameter through a common interface: "IVisible". For this, you need a GetVisible and a SetVisible, but you obviously also need the datamember "bool visible". If you don't have multiple inheritance, but can only use an interface, then you can put the "GetVisible" and "SetVisible" in the interface, but not their implementation. Everyone who uses the IVisible interface, has to implement it in his own way. Everyone has to duplicate the same code over and over: type a "bool visible" and make GetVisible/SetVisible use that. What if I want to enforce that everything that inherits from IVisible, has the same implementation of GetVisible and SetVisible and not make other users have to implement it in their own way? How to allow this even for classes that also inherit from other things that also need to have their own implementation? In C++ I can do that, since you can inherit from multiple classes that have some implementation in them. How can this be done in languages that don't have multiple inheritance? The Get/SetVisible was just an example. I'm sure there can be found examples where you'd really want to make sure that every class that inherits from the interface HAS to have the exact implementation you want to enforce.

Share this post


Link to post
Share on other sites
Advertisement
As I observe, If I consider to use IVisible to many class. those class would have some relationship (I mean somethings that look similar the same) so I will produce new class that implement IVisible and let other class inherit from this one.

But as you said, multiple inherit would allow us to implement adapter pattern ( can't remember the name class or object adapter) that quite useful

In my opinion, I think multiple inherit can produce some chaos in OOP so java consider to take it out.

Share this post


Link to post
Share on other sites
The only simple way to achieve this I can currently imagine of is to use a helper class.


interface IVisible { ... }

class VisibleImpl implements IVisible { /* implementation */ }

class SomeOtherClass implements IVisible
{
public getVisible() { return _vimpl.getVisible(); }
...

private final VisibleImpl _vimpl;
}



I don't think there's a way of forcing to use one specific implementation, except for final but AFAIK this can only work with superclasses, leading us back to square one.

Since the entire point of interfaces is to be able to provide different implementations for different objects, maybe you need to look elsewhere if you want to disallow this? If the same treatment must be guaranteed for all objects, you might as well just put it in a separate function/method in a static class. But then again, you're in trouble if you need to associate data with these objects. You could use some kind of map for this... Well, you can go as far as you want to enforce restrictions you want to impose, but there's no guarantee it can be done efficiently.

Share this post


Link to post
Share on other sites
The C++ I/O streams library has been implemented with multiple inheritance. There's some discussion regarding this in Artima titled Multiple Inheritance and Interfaces. A quote from the article:
... Notably, Jerry Schwartz, who had implemented the IO streams library based on multiple inheritance, had publicly said that if he had it to do over again he would not use multiple inheritance. He would find some way to get rid of the multiple inheritance...

Share this post


Link to post
Share on other sites
Isn't the standard alternative to multiple inheritance to use composition, and perhaps implement multiple interfaces?

Share this post


Link to post
Share on other sites
Quote:
Original post by d000hg
Isn't the standard alternative to multiple inheritance to use composition, and perhaps implement multiple interfaces?
Multiple inheritance or implementing multiple interfaces? Eh, I don't think I quite follow you now. [smile]

All this next stuff is a bit off-topic, but here is something that falls into my philosophy of constructing software systems, Virtuality by Herb Sutter. As I've done a lot of .NET programming in C# lately, I've found that giving a virtual function to someone, especially in a more performance critical or threaded environment, could be really bad. The function execution can literally travel around the world. [grin]

Anyway, you might also be interested on a series of articles that appeared in Artima in year 2002. The abstract:
A Conversation with Scott Meyers (August 2002) -- Scott Meyers, C++ expert and author of numerous books including Effective C++, talks with Bill Venners about many topics of object-oriented design, including the use of multiple inheritance in light of Java's interface, designing minimal and complete interfaces, public data members and contracts, and the utility of const.
Part I: Multiple Inheritance and Interfaces
Part II: Designing Contracts and Interfaces
Part III: Meaningful Programming
Part IV: Const, RTTI, and Efficiency
The base URL is Artima.com Interviews.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!