Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 389
Darf Zon Avatar asked Jan 25 '26 01:01

Darf Zon


1 Answers

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.

like image 177
Jordão Avatar answered Jan 27 '26 14:01

Jordão



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!