Overriding an abstract method with a virtual one

﹥>﹥吖頭↗ 提交于 2019-12-01 17:52:11

An override method is implicitly virtual (in the sense that it can be overridden in a subclass), unless marked as sealed.

Observe:

public class FirstLevelChild1 : TopLevelParent
{
    protected override void TheAbstractMethod() { }
}

public class SecondLevelChild1 : FirstLevelChild1
{
    protected override void TheAbstractMethod() { } // No problem
}

public class FirstLevelChild2 : TopLevelParent
{
    protected sealed override void TheAbstractMethod() { }
}

public class SecondLevelChild : FirstLevelChild2
{
    protected override void TheAbstractMethod() { } 
    // Error: cannot override inherited member 
    // 'FirstLevelChild2.TheAbstractMethod()' because it is sealed
}

An abstract method is already virtual all the way down the inheritance chain - there's no need to declare it virtual in a subclass to allow the subclass to override it - the subclass can already override it.

If you don't provide an implementation, the closest implementation (looking down the inheritance list) will be used.

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