|
-
Nov 4th, 2005, 07:55 AM
#1
Thread Starter
Member
Multiple Inheritance
What is the best way to achieve multiple inheritance in .NET ?
-
Nov 4th, 2005, 08:02 AM
#2
Re: Multiple Inheritance
Have one class to inherit from and several Interfaces.
-
Nov 16th, 2005, 03:43 PM
#3
Hyperactive Member
Re: Multiple Inheritance
Another option that is sometimes worth exploring is hierarchical inheritance.
Code:
ClassA
{
}
ClassB : ClassA
{
}
ClassC : ClassB
{
}//etc
Although each class is only inheriting from one parent that parent can inherit from another.
The benefit of this method is that the methods and properties that are inherited can be implemented in the preceding generations, unlike interfaces where no implementation is possible, so you have to implement every inherited method.
-
Nov 16th, 2005, 06:52 PM
#4
Re: Multiple Inheritance
C++ is the only language in Visual Studio that supports genuine multiple inheritence. Even then, I'm not sure but I have a feeling that you may only be able to use it in unmanaged code anyway. Implementing multiple Interfaces, as mendhak suggested, is as close as you can get to multiple inheritence in other Visual Studio languages, but while implementing an interface and inheriting a class are similar they are certainly not the same.
Local help: ms-help://MS.MSDNQTR.2003FEB.1033/vbls7/html/vblrfVBSpec4_2_2.htm
MSDN online: http://msdn.microsoft.com/library/de...BSpec4_2_2.asp
-
Nov 17th, 2005, 04:05 PM
#5
Hyperactive Member
Re: Multiple Inheritance
True multiple inheritance is either superb or a nightmare.
If you are programming in C++ and you are the only person writing the project and you never forget things, it is a real bonus.
However, if there is anyone else writing the classes you want to also inherit from it can become a minefield.
If I write a class with a method foo() and then inherit from it that's ok, if I then inherit from a class you've written and your class also has a method foo() we have problems.
Some compilers have a hissy fit, worse some don't, so you don't know which foo() is called, if either, or if you will get a run-time error.
So I think I prefer hierarchical inheritance and interfaces, and the little bit extra coding that entails.
-
Nov 17th, 2005, 05:21 PM
#6
Re: Multiple Inheritance
 Originally Posted by GlenW
True multiple inheritance is either superb or a nightmare.
If you are programming in C++ and you are the only person writing the project and you never forget things, it is a real bonus.
However, if there is anyone else writing the classes you want to also inherit from it can become a minefield.
If I write a class with a method foo() and then inherit from it that's ok, if I then inherit from a class you've written and your class also has a method foo() we have problems.
Some compilers have a hissy fit, worse some don't, so you don't know which foo() is called, if either, or if you will get a run-time error.
So I think I prefer hierarchical inheritance and interfaces, and the little bit extra coding that entails.
Absolutely. It's not just a twist of fate the Microsoft hasn't included multiple inheritence in the other Visual Studio languages. They have specifically omitted it to avoid those very issues. Multiple inheritence may be handy at times but it's too easy to mess up, so it creates more problems than it solves.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|