SRA
SRA

Reputation: 1691

Difference between multiple inheritance and interfaces in c#

Does c# really support multiple inheritance. People say it supports multiple inheritance in the form of interfaces ? But I dont think so

Upvotes: 1

Views: 2149

Answers (4)

Tomas Petricek
Tomas Petricek

Reputation: 243061

In theory of object oriented languages, there are two concepts that are often mixed together when talking about inheritance in C#/Java/etc.

Subtyping means that a single class can be written in a way that it can be casted to (or viewed as) some other simpler type (called supertype). In C# terms, this means that you can pass an object to a method where a parent class or an interface is expected. An object in C# can clearly have multiple supertypes (parent + as many interfaces as you want)

Subclassing means that a type inherits implementation from some other type. In C# this happens when you have a parent class, but not when you're implementing an interface (because you don't inherit any implementation from the interface). So, C# allows you to have only a single superclass (=parent class).

Upvotes: 1

Chandam
Chandam

Reputation: 653

The language designers decided not to allow multiple inheritance in C#. It has been discussed before.

Upvotes: 0

Samiksha
Samiksha

Reputation: 6182

Implementation over delegation (IOD) is a very simple coding techniques which allows a developers fast implementation of an system interfaces and multiple inheritance in C# (I know:It is not supported :))

Well the whole idea is basically to have a class field of desired type and to expose functionality public properties of the class based on hidden calls to the member field

So if we have something like Class A and Class B are parents of a Class C.An example of that situation could be the case when we would have a user control which should be able to handle and present a list of customers

Because user control and List are bot classes you couldn't do that directly, by inheriting, because there is no support for multiple inheritance in the case of c# (and I'm happy with that because I think multiple inheritance when not properly used very often leads to class explosion anti pattern)

Upvotes: 0

Adam Houldsworth
Adam Houldsworth

Reputation: 64487

In the literal sense, it does not support multiple inheritance. It can implement multiple interfaces, which offer polymorphic behaviour, so get you some benefits of multiple inheritance. However you get no base behaviour.

If you need the base behaviour a common tactic is for a base class to implement the interfaces and for derived classes to override this implementation where required.

I have yet to run into the need for multiple inheritance, I don't think C# suffers for the lack of it.

Upvotes: 7

Related Questions