Results 1 to 6 of 6

Thread: Multiple Inheritance

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2004
    Location
    Sunderland, UK
    Posts
    53

    Multiple Inheritance

    What is the best way to achieve multiple inheritance in .NET ?

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Multiple Inheritance

    Have one class to inherit from and several Interfaces.

  3. #3
    Hyperactive Member GlenW's Avatar
    Join Date
    Nov 2001
    Location
    Gateshead, England
    Posts
    479

    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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Hyperactive Member GlenW's Avatar
    Join Date
    Nov 2001
    Location
    Gateshead, England
    Posts
    479

    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.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Multiple Inheritance

    Quote 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width