6

As I am working as a C# developer, I know that we can implement multiple inheritance by use of Interface.

Can anybody please provide me link OR code for how to achieve multiple inheritance with C#.

I want code for how to achieve multiple inheritance in C# with the use of Interface.

Thanks in advance.

5
  • 8
    In C# you can implement multiple interfaces, but you can only inherit from one class. Commented Aug 13, 2010 at 5:30
  • 11
    When did implementing multiple interfaces become equal to multiple 'inheritance'? Implement and Inherit are two entirely different things. Commented Aug 13, 2010 at 5:30
  • @Hasan: not 'entirely', inheriting from an interface satisfies the substitution principle. Commented Aug 13, 2010 at 7:38
  • @Henk yes it does but there is a semantic difference. Interfaces represent behaviors and classes represent entities. Commented Aug 13, 2010 at 8:40
  • 1
    @Hasan / @Henk: Meyer defines sixteen types of (multiple) inheritance (p824+), but the here discussed difference between interfaces and classes is defined by only two: multiple implementation inheritance and multiple interface inheritance (or behavioral, or subtype inheritance, as Meyer calls it). C# only supports single implementation inheritance and multiple interface inheritance. Point being: interface inheritance is not equal to, but a form of multiple inheritance. Commented Aug 16, 2010 at 12:36

5 Answers 5

11

It's not strictly multiple inheritance - but your C# class can implement multiple interfaces, that's right:

public class MyClass : BaseClass, IInterface1, IInterface2, IInterface3
{
   // implement methods and properties for IInterface1
   ..

   // implement methods and properties for IInterface2
   ..

   // implement methods and properties for IInterface3
   ..
}

You need to provide implementations for all methods (and properties) defined in all of the interfaces you're planning to implement.

I'm not quite clear on what you are looking for from your question.... can you clarify a bit?? What are you trying to do? Where are you running into trouble??

Sign up to request clarification or add additional context in comments.

3 Comments

Marc, I want to know that How would we can get exact behavior in multiple inheritance with Interface just like we inherit single class. Would also like to know that can we cast our current object with another class with the use of Interface? (May be this is called MH)
you cannot get the exact same behavior with implementing interfaces as you would with true multiple inheritance. Interfaces only define a functional interface - you cannot inherit from multiple implementations. It's an approximation - not an exact substitute.
"strict multiple inheritance" does not exist, however some consensus seems to be that "strict" means multiple implementation inheritance. As it comes, multiple interface inheritance is just another form of multiple inheritance, of which many forms exist (Meyer describes 16 such forms p824+ in OOSC).
7

Here is a good example.

http://blog.vuscode.com/malovicn/archive/2006/10/20/How-to-do-multiple-inheritance-in-C_2300_-2D00-Implementation-over-delegation-_2800_IOD_2900_.aspx

A quick code preview:

interface ICustomerCollection
{
      void Add(string customerName);
      void Delete(string customerName);
}

class CustomerCollection : ICustomerCollection
{
      public void Add(string customerName)
      {
            /*Customer collection add method specific code*/
      }
      public void Delete(string customerName)
      {
            /*Customer collection delete method specific code*/
      }
}

class MyUserControl: UserControl, ICustomerCollection
{
      CustomerCollection _customerCollection=new CustomerCollection();

      public void Add(string customerName)
      {
            _customerCollection.Add(customerName);
      }
      public void Delete(string customerName)
      {
            _customerCollection.Add(customerName);
      }
}

Comments

7

Implementing multiple interfaces is NOT a replacement of multiple inheritance. Interfaces are there to specify the contract a class will adhere to. More like abstract classes.

If you want to achieve the effects of multiple inheritance, implementing Composite Pattern can help you.

Comments

2
class MyClass : IFirstInterface, ISecondInterface, IThirdInterface
{
  // implement the methods of the interfaces
}

Comments

1

Then there is this to inherit from more than one interface:

class EmptyClass : IDoSomething, IDoSomethingElse
{
}
interface IDoSomething
{
}
interface IDoSomethingElse
{
}

static class InterfaceExtensions
{
    public static int DoSomething(this IDoSomething tThis)
    {
        return 8;
    }
    public static int DoSomethingElse(this IDoSomethingElse tThis)
    {
        return 4;
    }
}

Using extension methods on the interface you can have something more like multiple inheritance than just tacking the interfaces on. Since the Methods are not part of the interface definition, you also don't have to implement them in the class, unless you want to. (Not really overriding them, you need a reference of type EmptyClass to call them since more specific, or exact type name, wins over inherited types.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.