Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's wrong with bodyless abstract methods in abstract class?

I'm refactoring a pre-existing solution. I use ReSharper and I've noticed a code inspection rule is being tripped. There is an abstract class which has bodyless method signatures with the intention of forcing the derived classes (of which there are several). As far as my knowledge goes, this is the (or at least a) correct way to do things. However, ReSharper is complaining that the "Type member is never accessed via base type" and that "Only overrides of [the methods] are used." Here is example code that replicates the issue in question:

public abstract class MyAbstractClass
{
    public abstract void CreateSomething();
    public abstract void ReadSomething();
    public abstract void InsertSomething();
}

public class MyDerivedClass : MyAbstractClass
{

    public override void CreateSomething()
    {
        throw new NotImplementedException();
    }

    public override void ReadSomething()
    {
        throw new NotImplementedException();
    }

    public override void InsertSomething()
    {
        throw new NotImplementedException();
    }
}

By the way, there are other members that rule out making the abstract class an interface. ReSharper suggests making changes to the 3 methods in the abstract class. Its suggestions are to make them protected, virtual, non-abstract or to simply remove them from the abstract class and only have them in derived classes. Whoever originally wrote this code intended for every derived class to implement these methods, and for those methods to be public in the derived classes. So, is there some way I should change this to make it more effective? If not, why is ReSharper taking issue with this?

like image 973
bubbleking Avatar asked Sep 27 '22 03:09

bubbleking


1 Answers

Because you are never accessing the method using a reference of type MyAbstractClass, there is no point in making it an abstract member--you could leave it out of the base class entirely and everything would compile just fine.

like image 141
DLCross Avatar answered Oct 03 '22 21:10

DLCross