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?

Community
  • 1
  • 1
USER_NAME
  • 1,029
  • 1
  • 14
  • 33
  • 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. – Thomas Levesque Sep 29 '12 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. – O. R. Mapper Sep 29 '12 at 19:54
  • @ThomasLevesque My question is straight that how multiple inheritance with the use of interface is useful? – USER_NAME Sep 29 '12 at 20:00
  • 1
    @Rasa, it's not useful, since it doesn't exist... Implementing multiple interfaces has nothing to do with multiple inheritance. – Thomas Levesque Sep 29 '12 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. – H H Sep 29 '12 at 20:45

2 Answers2

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.

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
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....

David W
  • 10,062
  • 34
  • 60
  • 1
    `IComparer` is not theoretical. Use `IComparable`. – nneonneo Sep 29 '12 at 19:54
  • @nneonneo: The `IComparer` interface mentioned in this answer is theoretical. – O. R. Mapper Sep 29 '12 at 19:55
  • 1
    Theoretical in the context of an example in the discussion. C'mon. – David W Sep 29 '12 at 19:55
  • I know, but the interface he's referring to *actually exists*. And no, I did not downvote him for that. – nneonneo Sep 29 '12 at 19:56
  • So no downvote if I had said, say, "IComparerizer" ? – David W Sep 29 '12 at 19:57
  • @nneonneo: No, it doesn't. The interface he's referring to is theoretical, as stated in the question, and with that said, it is totally irrelevant whether there is an actually existing interface with the same name. Some abstract thinking, please :-) Imagine, for example, he's not talking about the actual `System.Collections.IComparer`, but about a theoretical `DavidW.IComparer`. Case solved, right? – O. R. Mapper Sep 29 '12 at 19:59
  • I was only trying to point out that the answer would be clearer if he made reference to a real interface that actually existed, since C# had it (and thus there was no need to call the discussion theoretical). I was not meaning to criticize the answer. – nneonneo Sep 29 '12 at 20:02
  • @O.R.Mapper Such an awesome suggestion I modified my answer to reflect it. Much better than changing to "IComparinizerinator" :) – David W Sep 29 '12 at 20:02
  • @nneonneo: Ah, all right then. Sorry if I sounded harsh. Your first comments just sounded a bit like totally ignoring the possibility of conceiving a theoretical interface if an interface with the same name already existed in practice. – O. R. Mapper Sep 29 '12 at 20:03
  • @O.R.Mapper: No, it wasn't my intention. I just felt the answer would make more sense if worded using a real interface. You don't have to ask the reader to imagine a fictional construct if you can just make reference to a real thing. – nneonneo Sep 29 '12 at 20:05