Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

public abstract class FirstAbstract {     public abstract void SomeMethod(); }  public abstract class SecondAbstract : FirstAbstract {     public abstract override void SomeMethod();     //?? what sense does this make? no implementaion would anyway force the derived classes to implement abstract method? } 

Curious to know why C# compiler allows writing 'abstract override'. Isn't it redundant? Should be a compile time error to do something like this. Does it serve to some use-case?

Thanks for your interest.

like image 367
Manish Basantani Avatar asked Jan 18 '12 05:01

Manish Basantani


People also ask

What is abstract overriding?

Abstract methods don't have any implementation and require non-abstract derived classes to implement them. Of course they are allowed only in abstract classes. Override allows a method to override a virtual or abstract method from its base class.

How do you override an abstract?

We cannot create objects of an abstract class. To implement features of an abstract class, we inherit subclasses from it and create objects of the subclass. A subclass must override all abstract methods of an abstract class. However, if the subclass is declared abstract, it's not mandatory to override abstract methods.

Can you override an abstract method?

An abstract method is a method that is declared, but contains no implementation. you can override both abstract and normal methods inside an abstract class. only methods declared as final cannot be overridden.

What is the purpose of abstract class?

An abstract class cannot be instantiated. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.


2 Answers

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.

like image 125
BrokenGlass Avatar answered Sep 20 '22 10:09

BrokenGlass


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.

like image 39
ghord Avatar answered Sep 18 '22 10:09

ghord