Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I Enforce a Subclass to Implement an Interface?

Tags:

c#

I have a base class, say named B. I have two derived classes, D1 and D2.

Looks like:

public abstract class B
{
    public abstract void DoSomething();
}

public class D1 : B
{
    public override void DoSomething() { ... }
}

public class D2 : B
{
    public override void DoSomething() { ... }
}

Now, I created a new interface IDeepCopyable<T> which has one method Clone():

public interface IDeepCopyable<T>
{
    T Clone();
}

I want to force each subclass too implement this interface with the base class (i.e. IDeepCopyable<B>.

If I try to leave B's declaration as-is but just inherit from ('implement' is a more accurate term?) IDeepCopyable<T>, such as:

public abstract class B : IDeepCopyable<B>

Then to implement it in the derived classes (either implicitly or explicitly), the compiler gives me an error message "B does not implement interface member IDeepCopyable<B>.Clone()".

I can, of course, create an abstract method whose name is identical to the interface's method's name, but I find that ugly. Why to redeclare the method?

Can I, in any way, leave that as wanted?

I see that VS2019 has an option "Implement interface abstractly" so I think that the answer is no, but A) I want to be sure. B) If so, is there a design concept behind this behavior, or is it just a bug in C# design?

Thanks in advance.

like image 371
Chayim Friedman Avatar asked Dec 07 '19 21:12

Chayim Friedman


People also ask

Can a subclass implement an interface?

Yes, in that case all subclasses of Dog will have the additional interface basetype Pets . The methods you declare in Pets will then have to be implemented in Dog , since Dog (I assume from your example) is not abstract.

How many interfaces can a subclass implement?

A class can extends another class and/ can implement one and more than one interface.

WHAT CAN interfaces not enforce?

An interface may not declare instance data such as fields, auto-implemented properties, or property-like events. By using interfaces, you can, for example, include behavior from multiple sources in a class.

Do you need override when implementing interface?

If you have default method in an interface, it is not mandatory to override (provide body) it in the classes that are already implementing this interface. In short, you can access the default methods of an interface using the objects of the implementing classes.


1 Answers

I can, of course, create an abstract method whose name is identical to the interface's method's name, but I find that ugly.

That's the way to go. Your base class implements the interface. Hence it has to fulfil it. It can do this by implementing the method concretely (which is not desired here) or the base class can force its inheritors to do so by declaring the method abstractly.

You say that's ugly, well I think it's just explicit. Anyway, there's no way around this ;-)

like image 154
Jan Köhler Avatar answered Oct 22 '22 10:10

Jan Köhler