Darf Zon
Darf Zon

Reputation: 6378

When override an abstract method, is it correct that I set abstract again?

Maybe the title is confusing.

Let me put you an example:

public abstract class Base
{
    protected abstract void DoSomething();
}

public abstract class BaseA : Base
{
    protected abstract void DoSomething();
}

public class ClassA1 : BaseA
{
    protected override void DoSomething()
    {
        // do something!
    }
}

public class ClassA2 : BaseA
{
    protected override void DoSomething()
    {
        // do something!
    }
}

With this code, it's a similiar scenario from my real project. I have a base class. But I realized that base class needs to be abstract again, so the method DoSomething needs to be abstract again and I want to override it when I have the concrete class.

Is a good practice? Or are there a problem because I've set the method to abstract two times?

Upvotes: 3

Views: 120

Answers (1)

Jordão
Jordão

Reputation: 56457

You don't really need to declare the method on BaseA; all its subclasses will inherit it through its parent.

An abstract override is useful when you want to redefine a concrete method as abstract on a subclass.

Upvotes: 3

Related Questions