Question:
There are 2 classes. Class2
contains a public virtual method:
public virtual void fun()
{
Console.WriteLine("1");
}
Class1
inherits from Class2
and overwrites fun
class Class1 : Class2
{
public override void fun()
{
base.fun();
Console.WriteLine("2");
}
}
If you create an object Class1
and call fun
Class1 cl = new Class1();
cl.fun();
the console will display 1 and 2. However, if you remove override
and leave:
class Class1 : Class2
{
public void fun()
{
base.fun();
Console.WriteLine("2");
}
}
then the console will again display 1 and 2. Question: why do we need the override
keyword that does not affect the result in any way? It is clear that, according to the idea, we are rewriting virtual methods inside the bodies of the methods of the inheritors, but for this it is enough to write virtual
in the base class, and create the method of the same name in the inheritor and call base
. So it turns out that the word override
not needed?
Answer:
The override
keyword is needed not by the compiler (it is perfectly able to see that the method of the base class is virtual), but by us to prevent errors.
Look, suppose we had a couple of a virtual method and its overrides, and we renamed the virtual method, but forgot to rename the override. Or we changed the signature of the base method, and when changing the signature of the overridden method, we made an error in the order of the arguments. Or we simply implement a virtual method, but made a mistake in the signature.
If the override
keyword was not required, the compiler would perfectly swallow this code: well, the base method is virtual, and the new method does not override it, but simply defines a parallel method. But with the override
keyword, the compiler can raise the alarm: override
does not override any method, this is an obvious error.
In addition, the override
keyword helps the reader understand that this is a method in the base method's overlap chain and will be called polymorphically. Without it, you would have to go into all the base classes and peep there, if there is an accidental virtual method with the exact same signature.
As Class1.fun
Shimansky notes in the comments, if you omit the override
keyword in your example, then the Class1.fun
function will override by name, but not virtually override the base class function Class2.fun
(and the compiler will complain that you did not specify the new
keyword. which means that you do it deliberately, and not by mistake). There is no difference for a call on the exact type of an object, but there will be a difference if you call on a reference to the base type :
Class2 cl = new Class1();
cl.fun();
In this case, Class2.fun
and Class1.fun
are unbound functions, and despite being virtual
in Class2.fun
, only the function of the base class will be called.
The meaning of virtual functions is that the implementation of the virtual function from the derived class will be called by reference to the base class. (You may not know the exact type of an object at runtime.)