C# class using subclass does not implement inherited abstract member error

萝らか妹 提交于 2019-12-06 14:07:05

Shouldn't the use of a subclass be ok on the override method?

No. Aside from anything else, which implementation would you expect to be called if the caller provided an instance other than your subclass?

testBase t = new test();
t.LoadCRUD(new SomeOtherDMO()); // What would be called here?

You might well argue that it would make sense to be able to override the base method with a subclass method which is more general (e.g. with a parameter which is a superclass of the original parameter type, or with a return type which is a subclass of the original return type) but .NET doesn't allow either of these anyway. The parameter and return types of the overriding method have to match the original method exactly, at least after generic type parameter substitution.

It sounds like you may want to make your base type generic:

public abstract class TestBase<T> where T : TestDmoBase
{
    public abstract void LoadCrud(T dmo);
}

public class Test : TestBase<TestDmo>
{
    public override void LoadCrud(TestDmo dmo)
    {
        ...
    }
}

Note that you should follow .NET naming conventions, too - even in sample code.

No, in this case you have to exactly follow the signature of the abstract method, in order to provide valid override.

So, you have to write:

public class test : testBase
{
    override protected void LoadCRUD(testDMOBase dmo)  //BASE CLASS
    { }
}

Shouldn't the use of a subclass be ok on the override method?

No, method overrides must use the same parameter types as their original declarations.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!