Home > Article > Backend Development > The difference between method overriding and method hiding in C#
In C#, there are two mechanisms to redefine or provide new implementations of parent class methods through subclasses, these two mechanisms are called method overriding and method hiding. Now based on the way the methods are reimplemented we can differentiate them.
The following are the important differences between method overriding and method hiding.
Serial number | Keyword | Method override | Method hiding |
---|---|---|---|
1 | Definition | Method overriding is a mechanism to achieve polymorphism, where the parent class and the child class have the same method, including parameters and Signature that, when called with a subclass object, calls the implementation in the subclass. | On the other hand, method hiding can be defined as a technique where the user can redefine a method of a base class or parent class using the new keyword, thereby hiding the base class's main basic implementation of that particular method. |
2 | Accessing the parent class implementation | In method overriding, the subclass can access the implementation of the parent class method. | In method hiding, the implementation of the parent class method cannot be accessed through the subclass reference. |
3 | Modifier usage | In method overriding, use the override keyword. | In method hiding, use the new keyword to define a new implementation in the subclass. |
4 | Implementation type | In method overriding, the implementation type of the method is the object type. | However, in method hiding, the implementation type of the method is a reference type. |
5 | Reference from parent class to subclass | In method overriding, when the parent class reference variable points to the object of the subclass , which will call the overridden method in the subclass. | On the other hand, in method hiding, when the parent class reference variable points to the object of the child class, it will call the hidden method in the parent class. |
6 | Purpose | Method overriding only redefines the implementation of the method. | Method hiding can completely redefine methods. |
The above is the detailed content of The difference between method overriding and method hiding in C#. For more information, please follow other related articles on the PHP Chinese website!