-5

Why does an interface not contain any implementation for its members? Was there any other reason for it only declaring members apart from being able to use it as a sort of contract?

The question is not for what is Interface or why we need Interface. The Question is Why we have a design that contains only Declaration. I don't think there is lot of similar ques. I cant find a relevant ans, so raised a new post.

Vino
  • 111
  • 1
  • 8
  • Interfaces (in C#) are a very useful abstraction for contract only. If you really need a mix, you could look into abstract classes. – Joachim Isaksson Jun 01 '14 at 10:42
  • 2
    That edit effectively changed the question. – Meirion Hughes Jun 01 '14 at 10:48
  • 2
    Implementation inheritance is dangerous, lots of ways to shoot yourself in the foot. Or rather, handing the gun to the guy that derives from your class to let him shoot your leg off. Should a method be virtual or not? And if it is, do you expect the derived class to call your method or not? And if it calls your base method, should it do before or after the derived method runs? Interfaces do not have this problem. – Hans Passant Jun 01 '14 at 11:01
  • I don't think that this question is too broad. I would like to see @EricLippert weigh in here. I'm sure he'd have the canonical answer. – Enigmativity Jun 01 '14 at 11:39
  • @MeirionHughes - Can you explain why you think the question's meaning was changed? I've re-read the change at least twice and it seems to mean the same thing to me. – Enigmativity Jun 01 '14 at 11:41
  • The last sentence, in fact the question, asks about the purpose of interfaces when there is no implementation of members. That is the sentence that was changed and therefore putting me on the wrong foot. – Patrick Hofman Jun 01 '14 at 19:47
  • I asked the question like why we need such a reference type that contains only declaration. I cant able to find similar question. Could you please share if any related question already asked. – Vino Jun 03 '14 at 09:15

3 Answers3

3

First, the assertion that interface members cannot contain implementations is not entirely correct: although you cannot write an implementation in the interface itself, you can share implementations of interface "outside members" by placing them in an extension method for an interface. LINQ library derives a good deal of its power from an ability to provide method implementations for interfaces, not necessarily for classes.

In general, though, interfaces provide a mechanism of separating the contract from its implementations by defining what is done without specifying how it is done. This separation is very powerful, because it lets you share code that programs to an interface. For example, you can write code to filter a collection only once, and then reuse the same code for collections of multiple types.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • @dasblinkenlight - as with Patrick's answer there is nothing here about why interfaces don't have their own implementation. That's what the OP asked for. – Enigmativity Jun 01 '14 at 11:05
  • @Enigmativity "That's what the OP asked for" <<<= with all due respect, this is merely your interpretation of OP's question. Moreover, interfaces can provide "their own implementation" through extension methods. – Sergey Kalinichenko Jun 01 '14 at 11:15
  • @dasblinkenlight - Even the original text - "Why does interface doesn't contain any implementation for its members. Apart from, it can be used as a contract, was there any other reason to be have only declaration" - seems quite clear what he's asking. – Enigmativity Jun 01 '14 at 11:28
  • @dasblinkenlight - And besides, to say that extension methods provide an implementation for interfaces isn't at all correct. I just tried creating an extension method that provided an implementation for a member on an interface. The interface member takes precedence. You cannot provide implementations through extension methods. You can *extend* an interface through extension methods, but not provide an implementation for an existing member. – Enigmativity Jun 01 '14 at 11:32
2

There are two purposes for interfaces:

  • as you already said: for establishing a contract of the use of an object;
  • interfaces can also be used as marker. It does not need members for that.

Consider this sample:

public class X : IMustSerialize
{ }

This class implements the memberless interface IMustSerialize. When walking down all classes or instances you can check whether the class implements this interface and act accordingly.

Also see What is the purpose of a marker interface?.

Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • There is no good reason to use an interface as a marker..Use attributes for that.. – Amir Popovich Jun 01 '14 at 10:49
  • Compile time limiting of classes that serialise. Attributes don't stop you adding to object[] MaybeTheySerialise... – Meirion Hughes Jun 01 '14 at 10:51
  • Although I agree on it being a better practice to use attributes, it is possible to do so. – Patrick Hofman Jun 01 '14 at 10:56
  • @PatrickHofman - I would guess because the answer does not answer the question. The question specifically asked why interfaces do not have implementations for its members. It's not enough to say what the purpose of an interface is. You need to say why interfaces don't have implementations. – Enigmativity Jun 01 '14 at 11:03
  • 1
    Because the question has changed from. Why No Methods to why No method implementations... ( was downvoted too - I deleted). :/ – Meirion Hughes Jun 01 '14 at 11:03
2

It was decided very early on in the design of .NET to not allow for multiple inheritance.

Had they allowed it then this would have been legal:

class A
{
    void Blah() { /* body */ }
}

class B
{
    void Blah() { /* body */ }
}

class C : A, B
{
}

The problem is which Blah should class C inherit.

This problem is basically the same if interfaces had implementations. It would become this:

class IA
{
    void Blah() { /* body */ }
}

class IB
{
    void Blah() { /* body */ }
}

class C : IA, IB
{
}

Instead by not allowing interfaces to have bodies then interfaces become a safe mechanism to allow objects to have polymorphism similar to multiple inheritance without any of the issues associated with full multiple inheritance.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172