0

Possible Duplicate:
Why is Multiple Inheritance not allowed in Java or C#?

Question: Do C# support multiple inheritance?

Answer: Yes, with the use of interface.

Now My Qusetion: If we inherit a interface then we have to implement it. So we are writing our own code then how we are using the core concept of inheritance that is reusibility by inheriting interface.

So it seens to be that multiple inheritance via interface is useless. If I am wrong then how multiple inheritance via interface is useful?

5
  • 6
    Implementing multiple interfaces is not multiple inheritance. So no, C# does not support multiple inheritance. You don't inherit an interface, you implement it. Commented Sep 29, 2012 at 19:51
  • What does your question have to do with implementing multiple interfaces? It seems that your question is rather about the purpose of implementing interfaces (even if it's just one) in the first place, not specifically to implementing several of them. Commented Sep 29, 2012 at 19:54
  • @ThomasLevesque My question is straight that how multiple inheritance with the use of interface is useful? Commented Sep 29, 2012 at 20:00
  • 1
    @Rasa, it's not useful, since it doesn't exist... Implementing multiple interfaces has nothing to do with multiple inheritance. Commented Sep 29, 2012 at 20:03
  • @ThomasLevesque - interfaces are frequently described as a 'lightweight' form of multiple inheritance. And while it certainly isn't that, it fulfills a few of the requirements, most noticeably the substitution principle. Commented Sep 29, 2012 at 20:45

2 Answers 2

5

You're confusing two different concepts:

  • class inheritance, where your class inherits the members and behavior of the base class. You can only have one base, C# does not support multiple inheritance
  • interface implementation, which is a way to express a contract fulfilled by your class. You can implement as many interfaces as you want.

So if you expect to inherit behavior by implementing an interface, of course it seems useless... implementing an interface is only a way to tell others "hey, I know how to do (something)". It doesn't automatically provide the implementation of that "something", that part is up to you.

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

1 Comment

Thanks for clearing my misconception.
0

Implementing an interface is creating an object that is guaranteed to perform to a particular "contract" of functions and methods, making it usable by any method that expects an object implementing that interface.

Consider a theoretical DavidW.IComparer interface for a Sort function. A generic sort could expect an object that supports an DavidW.IComparer interface that provide the comparison between two objects. An implementation of DavidW.IComparer provides the specific implementations that define how two objects being sorted relate in the consumer's problem domain.

Interfaces define a contract, implementors provide the literal plumbing. That's where the value lies. And, in reality, you don't "inherit" an interface....

10 Comments

IComparer is not theoretical. Use IComparable<T>.
@nneonneo: The IComparer interface mentioned in this answer is theoretical.
Theoretical in the context of an example in the discussion. C'mon.
I know, but the interface he's referring to actually exists. And no, I did not downvote him for that.
So no downvote if I had said, say, "IComparerizer" ?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.