Difference between Method Overriding and Method Hiding in C#
Method Overriding is a technique that allows the invoking of functions from another class (base class) in the derived class. Creating a method in the derived class with the same signature as a method in the base class is called Method Overriding.
In simple words, Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. When a method in a subclass has the same name, same parameters or signature and the same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class.
My new parent class.. ! My new child class.. !
In Method Hiding, you can hide the implementation of the methods of a base class from the derived class using the new keyword. Or in other words, in method hiding, you can redefine the method of the base class in the derived class by using the new keyword.
This is my child class.
Method Overriding Vs Method Hiding
|Method overriding||Method hiding|
|In method overriding, you need to define the method of a parent class as a virtual method using virtual keyword and the method of child class as an overridden method using override keyword.||In method hiding, you just simply create a method in a parent class and in child class you need to define that method using new keyword.|
|It only redefines the implementation of the method.||In method hiding, you can completely redefine the method.|
|Here overriding is an object type.||Here hiding is a reference type.|
|If you do not use override keyword, then the compiler will not override the method. Instead of the overriding compiler will hide the method.||If you do not use the new keyword, then the compiler will automatically hide the method of the base class.|
|In method overriding, when base class reference variable pointing to the object of the derived class, then it will call the overridden method in the derived class.||In the method hiding, when base class reference variable pointing to the object of the derived class, then it will call the hidden method in the base class.|