0

Using C# for a while, I've just got curious about overriding abstract methods. I'm NOT asking "Why we have to override every abstract method?", but asking "Why we use override keyword on implementing abstract method?".

Abstract class were really useful when I had to provide the basic functionality of classes. Since Unity doesn't support C# 8.0 yet, the only choice to provide default method was to make abstract classes :(

public interface ISomething
{
    void DoSomething();
}

public class A : ISomething
{
    public void DoSomething() { }
}

// ----------------------------------

public abstract class Something
{
    public abstract void DoSomething();
{

public class B : Something
{
    public override void DoSomething() { }  // WHY override to only this??
}

What I thought to be weird is, there is literally nothing to override in abstract method since there is no function body, just like interface method. Then why I should write override keyword? Is there any purpose for it? Or Was there any inevitable reason to do so?

MyBug18
  • 2,135
  • 2
  • 11
  • 25
  • 3
    Implementing interface != inheriting from abstract class. – Sinatr Jul 23 '20 at 08:39
  • 4
    Only one of the C# language designers (e.g. Anders Hejlsberg) could really answer this question - anything else will just be an opinion. – Matthew Watson Jul 23 '20 at 08:40
  • Because with `public override void DoSomething() { }` in `B` you are overwriting the definition of `DoSomething` in `Something` (that definition might by an abstract one, but it is overridden none the less) – Ackdari Jul 23 '20 at 08:47
  • I don't think the duplicate question is actually a duplicate, since that is specifically asking about redeclaring an abstract method in a derived abstract class. – Matthew Watson Jul 23 '20 at 08:55
  • 1
    One of the main purposes for wanting `override` *at all* is to prevent the danger of introducing a new method in the base class silently making a previously unique method in a derived class now suddenly be an implementation of that new method, with no compiler errors, if the names/parameters match. – Damien_The_Unbeliever Jul 23 '20 at 08:57

0 Answers0