160

My test code in C#:

namespace DSnA
{
    public abstract class Test : IComparable
    {

    }
}

Results in the following compiler error:

error CS0535: 'DSnA.Test' does not implement interface member
'System.IComparable.CompareTo(object)'

Since the class Test is an abstract class, why does the compiler require it to implement the interface? Shouldn't this requirement only be compulsory for concrete classes?

bguiz
  • 27,371
  • 47
  • 154
  • 243
  • Haha. I wrote one thing then decided to change it. Sorry. :) – Joel Apr 24 '10 at 20:26
  • 6
    Based on the downvotes and comments on the accepted answer, I believe the downvotes come because of the way the question is worded. OP asks "why is it this way", which would be outside stackoverflow's scope. Having encountered this myself, the question is more like "Am I missing something? Do I really have to supply implementations? Doesn't that defeat the point of it being an abstract class?" To which the answer is "No, you don't have to supply *implementations* (which would violate the purpose of an abstract class), but here is what you do have to do, to make your situation work." – ToolmakerSteve Feb 04 '17 at 23:25
  • I found a case where you do have to supply an implementation. It is where the interface has an optional parameter. If you include the method as abstract in the base class then the inherited classes will not compile without the optional parameter (which defeats the purpose of an optional parameter). I just throw NotImplementedException in this case. – Paul McCarthy Feb 03 '20 at 14:03
  • Ignore my previous comment - it didn't work as expected, principle of least surprise doesn't apply here. – Paul McCarthy Feb 03 '20 at 14:11

3 Answers3

189

In C#, a class that implements an interface is required to define all members of that interface. In the case of an abstract class, you simply define those members with the abstract keyword:

interface IFoo
{
    void Bar();
}

abstract class Foo : IFoo
{
    public abstract void Bar();
}

Or to put it another way: you don't have to "implement" it (which would be a terrible limitation on abstract classes); however, in C#, you do have to tell the compiler that you are deliberately passing the buck to concrete subclasses - and the above line of code shows how to do so.

The comments and downvotes complaining that this is not an answer to the question are missing the point. Someone coming to Stack Overflow, having received this compiler error, but having an abstract class in which it would be a mistake to supply an implementation, are stuck without a good solution - would have to write implementation methods that threw runtime exceptions, a horrendous work-around - until they have the above information. Whether it is good or bad that C# requires this explicitness is outside the scope of Stack Overflow, and not relevant to the question nor this answer.

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
Joel
  • 16,474
  • 17
  • 72
  • 93
  • I am looking for a similar answer but in my case I have 2 interfaces (e.g. IFoo1 and IFoo2) with the same method name and am having some problems marking them as abstract in my base (abstract) class. Can you help? – Ben Jun 07 '12 at 15:37
  • Does not explain why abstract inheritor does not need to implement base' abstract members. – AgentFire Aug 30 '12 at 11:18
  • 2
    @Ben Just saw your comment. You probably have figured it out already, but in case someone else needs it. Check out Explicit Interface Implementations: http://msdn.microsoft.com/en-us/library/ms173157.aspx – Joel Aug 30 '12 at 14:37
  • 2
    @Joel @Ben I don't think explicit interfaces can work with abstract classes. In the example code above, change the definition in `Foo` to `public abstract void IFoo.Bar();` and you get complaints that "public" and "abstract" are not valid modifiers. – Darren Cook Nov 12 '12 at 00:46
  • 12
    This does not answer the question of why this is even necessary at all, considering this is an abstract class and the compiler should know how to fill in the blank. In Java this is not necessary, which allows for several useful patterns such as decorator pattern on ioc containers e.g. Spring/JavaEE (when you need to decorate a particular method of a managed interface). The same implementation in.net would have to force developers to be very verbose especially on big interfaces such as nhibernate's ISession – Sheepy Jan 09 '15 at 08:15
  • 1
    AspectJ's mixins is another example. It allows you to mix partial implementations from many abstract classes into a single interface. Each abstract class only needs to implement the method it does want to implement. No dumb abstract method boilerplate getting in the way as is the case if i'm to recreate the same functionality in .net – Sheepy Jan 09 '15 at 08:21
  • 1
    @Sheepy - True, but, IMHO, you misunderstand *what the asker needs*, and how this is, indeed, an "answer". I likewise had the same question - because it did not make sense to be required to supply an implementation, so I was stuck. The answer is: You *don't* have to "*implement*" it - but here is what you *do have to do* to tell the compiler that you aren't going to implement it. (The question that you [correctly] say this is not an answer to, would not be an appropriate stackoverflow question - it would have simply been closed as off-purpose.) – ToolmakerSteve Feb 04 '17 at 23:00
15

Unlike Java, in C#: "an abstract class must provide implementations of all members of the interfaces that are listed in the base class list of the class. However, an abstract class is permitted to map interface methods onto abstract methods."

https://msdn.microsoft.com/en-us/library/Aa664595(v=VS.71).aspx

00jt
  • 2,818
  • 3
  • 25
  • 29
  • 1
    Super clear answer and great that you provide both situations, as you also might sometimes want to implement the behaviour in the base class – VinKel Jan 30 '17 at 11:03
  • One questions that arises here is: why these C# boilerplate declarations (which obviously they are) need to exist in abstract classes, which could otherwise be concise and shorter (thus messing the classes)? In my C# project I have lots of abstract classes and interfaces - and what I do for most time is copying & pasting methods declarations in Visual Studio. – forsberg Jun 11 '17 at 17:18
7

They don't have to actually implement the interface.
The interface methods/properties can be abstract or even virtual as well. So its up to the subclasses to actually implement them.

ntziolis
  • 10,091
  • 1
  • 34
  • 50