1

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

0

4 Answers 4

7

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.

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

4 Comments

I was about to add this and your answer came through. Multiple Inheritance is not supported in C#. Since Interfaces do not define a default behaviour. However, you can inherit from multiple contracts (Interfaces).
Multiple object inheritance is not supported in the CLR either. I have seen places were multiple inheritannce would be nice... but I it forces you do create objects that are easier for others to maintain.
The only time I've been bitten by this is when a type doesn't extend MarshalByRefObject.
@Will ah yea, forgot about that one - I've had that once before, but only the once :)
1

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).

Comments

0

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)

Comments

0

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

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.