Why do abstract classes need to define abstract methods from interfaces they implement?
abstract, c#, interface
Solution
An abstract class is a fully-fledged type, just except it cannot be instantiated. Hence its full contract must be declared even though some of its methods are not implemented. A user of a particular abstract class must be able to bind to all its methods, be they coming from interfaces or directly declared in the abstract class. It the methods from interfaces were not (at least) declared (if not even implemented) in the abstract class, the used couldn't bind to them.
Further, classes and interfaces are somewhat loosely-coupled. A class may declare a method, which is later mapped to a same-signature method in an interface which is implemented by its descendant. Hence it is again a “good idea” from the language-design viewpoint to require all methods of interfaces being directly implemented on an abstract class to be actually declared in it.
You can think of an interface as a detachable feature (except when explicit implementations are used). The abstract class may live on its own and its direct users need not to know of any of its interfaces.
It's a design feature that only virtual methods can be left without an implementation, because the technical mechanism of virtual methods needs to be leveraged for the whole concept of abstract classes work in practice.
UPDATE: Example:
public interface IFoo { void M(); }
public abstract class Bar : IFoo
{
public virtual abstract void M();
public void N() { }
}
public class Baz : Bar
{
public override void M() { … }
}
…
public void Method(Bar par)
{
par.M();
}
…
Baz x = new Baz();
Method(x);
The `Method` see the instance denoted by the variable `x` as `Bar` — neither as `Baz` nor as `IFoo`. In other words, the user of class `Bar` does not care of whether it implemented one, two, ten, or no interface at all. All it does is access its members. It rely's on `Bar`'s contract, not of `IFoo`s contract. Hence if `Bar` implements `IFoo`, it must define all members from `IFoo`.
Problem
When an abstract class implements an interface, it is required to also either define or declare the methods (as asked before): ``` public interface MyInterface { void Method(); } public abstract class MyAbstractClass : MyInterface { public abstract void Method(); // required, even though MyAbstractClass does not implement } public class MyClass : MyAbstractClass { public override void Method() { } } public interface MyInterface2 : MyInterface { // no need to declare Method() -- why can't abstract classes do the same for unimplemented methods? } ``` What is the design rationale of the c# language to require the definition of abstract methods of abstract classes that implement interfaces? It seems completely redundant for an abstract class to be required to define a method that it does not implement (and to make it even worse, for the class that actually implements the method to have to mark the method as override). I see no reason why an abstract class could not behave like MyInterface2, which inherits from MyInterface but does not need to declare MyInterface's methods.