What is the use of 'abstract override' in C#?

前端 未结 7 967
梦如初夏
梦如初夏 2020-11-27 17:44

Just out of curiosity I tried overriding a abstract method in base class, and method the implementation abstract. As below:

public abstract class FirstAbstra         


        
相关标签:
7条回答
  • 2020-11-27 18:11

    I find it really useful for ensuring proper ToString() implementation in derived classes. Let's say you have abstract base class, and you really want all derived classes to define meanigful ToString() implementation because you are actively using it. You can do it very elegantly with abstract override:

    public abstract class Base
    {
        public abstract override string ToString();
    }
    

    It is a clear signal to implementers that ToString() will be used in base class in some way (like writing output to user). Normally, they would not think about defining this override.

    0 讨论(0)
  • 2020-11-27 18:19

    Imagine that SecondAbstract is in the middle of a three-class hierarchy, and it wants to implement some abstract methods from its base FirstAbstract while leaving some other method X to be implemented from its child ThirdAbstract.

    In this case, SecondAbstract is forced to decorate the method X with abstract since it does not want to provide an implementation; at the same time, it is forced to decorate it with override since it is not defining a new method X, but wants to move the responsibility of implementing X to its child. Hence, abstract override.

    In general, the concepts modelled by abstract and override are orthogonal. The first forces derived classes to implement a method, while the second recognizes that a method is the same as specified on a base class and not a new one.

    Therefore:

    • neither keyword: "simple" method
    • abstract only: derived class must implement
    • override only: implementation of method defined in base class
    • abstract override: derived class must implement a method defined in base class
    0 讨论(0)
  • 2020-11-27 18:23

    There's a useful example for this on Microsoft Docs - basically you can force a derived class to provide a new implementation for a method.

    public class D
    {
        public virtual void DoWork(int i)
        {
            // Original implementation.
        }
    }
    
    public abstract class E : D
    {
        public abstract override void DoWork(int i);
    }
    
    public class F : E
    {
        public override void DoWork(int i)
        {
            // New implementation.
        }
    }
    

    If a virtual method is declared abstract, it is still virtual to any class inheriting from the abstract class. A class inheriting an abstract method cannot access the original implementation of the method—in the previous example, DoWork on class F cannot call DoWork on class D. In this way, an abstract class can force derived classes to provide new method implementations for virtual methods.

    0 讨论(0)
  • 2020-11-27 18:24

    This is done because in child class you can not have abstract method with same name as in base class. override tells compiler that you are overriding the behavior of base class.

    Hope this is what you are looking for.

    0 讨论(0)
  • 2020-11-27 18:26

    This design pattern is known as the Template Method pattern.

    Wikipedia page on Template Methods

    A simple, non-software example: There are a bunch of military units: tanks, jets, soldiers, battleships, etc. They all need to implement some common methods but they will implement them very differently:

    • Move()
    • Attack()
    • Retreat()
    • Rest()

    etc...

    0 讨论(0)
  • 2020-11-27 18:27

    Interestingly enough, the Roslyn version of the C# compiler has an abstract override method in it, which I found odd enough to write an article about:

    http://ericlippert.com/2011/02/07/strange-but-legal/

    0 讨论(0)
提交回复
热议问题