10

When we create a class that inherits from an abstract class and when we implement the inherited abstract class why do we have to use the override keyword?

public abstract class Person
{
    public Person()
    {

    }

    protected virtual void Greet()
    {
        // code
    }

    protected abstract void SayHello();
}

public class Employee : Person
{
    protected override void SayHello() // Why is override keyword necessary here?
    {
        throw new NotImplementedException();
    }

    protected override void Greet()
    {
        base.Greet();
    }
}

Since the method is declared abstract in its parent class it doesn't have any implementation in the parent class, so why is the keyword override necessary here?

2
  • 2
    An abstract method is implicitly a virtual method, per specs Dec 20, 2019 at 16:17
  • 3
    Because if you don't put it there, you are "hiding" the method instead of overriding it. That's why you get the warning that "if that is what you intended, use the new keyword`... You'd also get the "no method overrides base class method" error.
    – Ron Beyer
    Dec 20, 2019 at 16:19

4 Answers 4

17

When we create a class that inherits from an abstract class and when we implement the inherited abstract class why do we have to use the override keyword?

"Why?" questions like this can be hard to answer because they are vague. I'm going to assume that your question is "what arguments could be made during language design to argue for the position that the override keyword is required?"

Let's start by taking a step back. In some languages, say, Java, methods are virtual by default and overridden automatically. The designers of C# were aware of this and considered it to be a minor flaw in Java. C# is not "Java with the stupid parts taken out" as some have said, but the designers of C# were keen to learn from the problematic design points of C, C++ and Java, and not replicate them in C#.

The C# designers considered overriding to be a possible source of bugs; after all, it is a way to change the behaviour of existing, tested code, and that is dangerous. Overriding is not something that should be done casually or by accident; it should be designed by someone thinking hard about it. That's why methods are not virtual by default, and why you are required to say that you are overriding a method.

That's the basic reasoning. We can now go into some more advanced reasoning.

StriplingWarrior's answer gives a good first cut at making a more advanced argument. The author of the derived class may be uninformed about the base class, may be intending to make a new method, and we should not allow the user to override by mistake.

Though this point is reasonable, there are a number of counterarguments, such as:

  • The author of a derived class has a responsibility to know everything about the base class! They are re-using that code, and they should do the due diligence to understand that code thoroughly before re-using it.
  • In your particular scenario the virtual method is abstract; it would be an error to not override it, and so it is unlikely that the author would be creating an implementation by accident.

Let's then make an even more advanced argument on this point. Under what circumstances can the author of a derived class be excused for not knowing what the base class does? Well, consider this scenario:

  • The base class author makes an abstract base class B.
  • The derived class author, on a different team, makes a derived class D with method M.
  • The base class author realizes that teams which extend base class B will always need to supply a method M, so the base class author adds abstract method M.
  • When class D is recompiled, what happens?

What we want to happen is the author of D is informed that something relevant has changed. The relevant thing that has changed is that M is now a requirement and that their implementation must be overloaded. D.M might need to change its behaviour once we know that it could be called from the base class. The correct thing to do is not to silently say "oh, D.M exists and extends B.M". The correct thing for the compiler to do is fail, and say "hey, author of D, check out this assumption of yours which is no longer valid and fix your code if necessary".

In your example, suppose the override was optional on SayHello because it is overriding an abstract method. There are two possibilities: (1) the author of the code intends to override an abstract method, or (2) the overriding method is overriding by accident because someone else changed the base class, and the code is now wrong in some subtle way. We cannot tell these possibilities apart if override is optional.

But if override is required then we can tell apart three scenarios. If there is a possible mistake in the code then override is missing. If it is intentionally overriding then override is present. And if it is intentionally not overriding then new is present. C#'s design enables us to make these subtle distinctions.

Remember compiler error reporting requires reading the mind of the developer; the compiler must deduce from wrong code what correct code the author likely had in mind, and give an error that points them in the correct direction. The more clues we can make the developer leave in the code about what they were thinking, the better a job the compiler can do in reporting errors and therefore the faster you can find and fix your bugs.

But more generally, C# was designed for a world in which code changes. A great many features of C# which appear "odd" are in fact there because they inform the developer when an assumption that used to be valid has become invalid because a base class changed. This class of bugs is called "brittle base class failures", and C# has a number of interesting mitigations for this failure class.

3
  • 4
    Thanks for elaborating. I always appreciate your answers, both because it's good to have an authoritative voice from someone "on the inside" and because you do a great job of explaining things simply and completely. Dec 21, 2019 at 4:36
  • Thank you for the in depth explanation! really appreciate it.
    – psj01
    Dec 23, 2019 at 4:48
  • Great points! Could you list some of the other mitigations that you mention?
    – aksh1618
    Jan 17, 2020 at 19:50
3

It's to specify whether you're trying to override another method in the parent class or create a new implementation unique to this level of the class hierarchy. It's conceivable that a programmer might not be aware of the existence of a method in a parent class with exactly the same signature as the one they create in their class, which could lead to some nasty surprises.

While it's true that an abstract method must be overridden in a non-abstract child class, the crafters of C# probably felt it's still better to be explicit about what you're trying to do.

1
  • This is a good start on understanding the reasoning of the language design team; I've added an answer which shows how the team starts with the idea you've expressed here but takes it a step farther. Dec 20, 2019 at 20:21
1

Because abstract method is a virtual method with no implementation, per C# language specification, means that abstract method is implicitly a virtual method. And override is used to extend or modify the abstract or virtual implementation, as you can see here

To rephrase it a little bit - you use virtual methods to implement some kind of late binding, whereas abstract methods force the subclasses of the type to have the method explicitly overridden. That's the point, when method is virtual, it can be overridden, when it's an abstract - it must be overriden

1
  • 1
    I think the question is not regarding what it does, but why it has to be explicit. Dec 20, 2019 at 16:31
0

To add to @StriplingWarrior's answer, I think it was also done to have a syntax that is consistent with overriding a virtual method in the base class.

public abstract class MyBase
{
    public virtual void MyVirtualMethod() { }

    public virtual void MyOtherVirtualMethod() { }

    public abstract void MyAbtractMethod();
}

public class MyDerived : MyBase
{
    // When overriding a virtual method in MyBase, we use the override keyword.
    public override void MyVirtualMethod() { }

    // If we want to hide the virtual method in MyBase, we use the new keyword.
    public new void MyOtherVirtualMethod() { }

    // Because MyAbtractMethod is abstract in MyBase, we have to override it: 
    // we can't hide it with new.
    // For consistency with overriding a virtual method, we also use the override keyword.
    public override void MyAbtractMethod() { }
}

So C# could have been designed so that you did not need the override keyword for overriding abstract methods, but I think the designers decided that would be confusing as it would not be consistent with overriding a virtual method.

1
  • Re: "the designers decided that would be confusing as it would not be consistent with overriding a virtual method" -- yes, but more. Suppose you have a base class B with a virtual method M and a derived class D with an override. Now suppose the author of B decides to make M abstract. That's a breaking change, but maybe they do so. Question: should the author of D be required to remove the override? I think most people would agree that it is absurd to force the author of D to make an unnecessary code change; their class is fine! Dec 20, 2019 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.