Multiple Inheritance in VB.NET

Multiple Inheritance in VB.NET

Old forum URL: forums.lhotka.net/forums/t/2562.aspx


PStanev posted on Tuesday, March 20, 2007

 Did someone know, if the only choice for Multiple Inheritance in VB.NET is using a interfaces. I would like to inherit from multiply base classes and have good code reuse. I think this is possible in C++. I want to use VB.NET 2005

Curelom replied on Tuesday, March 20, 2007

Neither VB.NET nor C# allow for multiple inheritance.  The solution is to use interfaces instead.  While interfaces have their issues, this eliminates problems with multiple inheritance including inheriting from objects that both contain the same property.  Which property should the child object use?

Bayu replied on Tuesday, March 20, 2007

Curelom:
Neither VB.NET nor C# allow for multiple inheritance.  The solution is to use interfaces instead.  While interfaces have their issues, this eliminates problems with multiple inheritance including inheriting from objects that both contain the same property.  Which property should the child object use?


There are a bunch of design techniques that could be leveraged though to achieve the same goal of optimal code reuse ....

In my experience multiple inheritance is most often (mis)used in c++ to merge a bunch of behavior/logic into one object. The same can be achieved using composition (although this is merely a structural pattern than a behavioral one, however you could 'compose' behaviors) or using commands where you put every bit of logic that you would like to reuse in a separate command that can be used by any object interested (resembles strategy pattern, only reuse is the driving force instead of variation).

Bayu

PStanev replied on Tuesday, March 20, 2007

Using interfaces is taking more code to achive inheritance. I wish there is a beter way.Sad [:(]

Bayu replied on Tuesday, March 20, 2007

I wasn't talking about interfaces, I was talking about concrete, fully implemented classes.

What I meant to say basically is that you can factor out those behaviors that you would like to reuse into their own hierarchy. Objects in need of the behavior you factored out this way can plug-in whatever they need following composition, strategy or command handler patterns

Bayu

SonOfPirate replied on Tuesday, March 20, 2007

The better way, and my suggestion, is to use collaboration to achieve the code reuse you are after.  For instance, and as shown in Rocky's implementation, when designing a class that implements ICloneable, instead of rewriting the same block of code, delegate to another class for the work (ObjectCloner).  This can be done in many cases to give you the benefits of code reuse but, alas, not all.  In these cases there are many other suggested approaches, as outlined in the other posts, that may help you achieve your goal.  You will find that no one pattern fits across all cases, it is finding the right mix that will get you the farthest.

HTH

 

Copyright (c) Marimer LLC