C#: Abstract classes need to implement interfaces?
Solution 1
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.
Solution 2
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
Solution 3
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.
Comments
-
bguiz about 4 years
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?-
Joel about 14 yearsHaha. I wrote one thing then decided to change it. Sorry. :)
-
ToolmakerSteve over 7 yearsBased 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."
-
Paul McCarthy over 4 yearsI 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 over 4 yearsIgnore my previous comment - it didn't work as expected, principle of least surprise doesn't apply here.
-
-
Ben almost 12 yearsI 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?
-
AgentFire over 11 yearsDoes not explain why abstract inheritor does not need to implement base' abstract members.
-
Joel over 11 years@Ben Just saw your comment. You probably have figured it out already, but in case someone else needs it. Check out Explicit Interface Implementations: msdn.microsoft.com/en-us/library/ms173157.aspx
-
Darren Cook over 11 years@Joel @Ben I don't think explicit interfaces can work with abstract classes. In the example code above, change the definition in
Foo
topublic abstract void IFoo.Bar();
and you get complaints that "public" and "abstract" are not valid modifiers. -
Sheepy over 9 yearsThis 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 over 9 yearsAspectJ'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
-
VinKel over 7 yearsSuper clear answer and great that you provide both situations, as you also might sometimes want to implement the behaviour in the base class
-
ToolmakerSteve over 7 years@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.)
-
forsberg almost 7 yearsOne 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.