Here, we will learn what is method hiding in c#, uses of method hiding in c#, and how to implement method hiding in c# with real-time examples.
In c#, method hiding or shadowing is a feature to implement the base class methods with same signature in the derived classes without taking permission from the base class.
By implementing method hiding, if we create an instance of the derived class, the derived class will hide the base class method implementation and execute the derived class method.
To implement method hiding or shadowing in c#, we need to define the base class methods with same signature in the derived class using a new
keyword.
Following is the syntax of implementing method hiding/shadowing in c# using a new
keyword.
In this method hiding syntax, we are hiding the base class (Vehicle) method (GetVehicleDetails) by defining the same method (GetVehicleDetails) in the derived class (Car) using a new
keyword.
By defining the derived class method with the new
keyword, it will let the compiler know that we are intentionally hiding the base class method.
Generally, the method hiding/shadowing in c# is same as the method overriding in c# but the only difference is in method overriding, the base class methods that we want to override or re-implement in the derived class will define with the virtual
keyword. In method hiding, we don’t need to define any method with virtual
in base class instead we can define the methods in the derived class with a new
keyword.
Following is the example of implementing the method hiding/shadowing in c#.
In this method hiding example, we created a base class Vehicle with a method GetVehicleDetails(). In the derived class Car we are hiding the base class Vehicle method by creating the same method GetVehicleDetails using a new
keyword.
In the Main method, we created an instance of the base class (Vehicle) and child class (Car) and called the GetVehicleDetails method. We also created an instance of Car and assigned it to the variable of type Vehicle. By using the Vehicle type variable, when we make a call to the GetVehicleDetails method, the base class method will be executed because the variable is of type Vehicle will not access any members which are re-implemented inside the child class.
When you execute this example, you will get the following result.
If you observe the result, the base class variable (carVehicle) executed the base class method even though we assign a child class instance. This happens because the base class variable cannot access any members that are purely defined inside the child class. The purely defined members mean the members that are re-implemented inside the child class using the hiding approach.
The answer is NO. In c#, we can also hide the base class methods by creating the methods with same signature in the derived class without using a new
keyword. If we are not using a new
keyword to hide the base class method, the compiler will throw a warning message as shown below.
The warning message won’t stop you from executing the application, but the compiler will throw a warning message to let you know whether you are intentionally hiding the base class method or mistakenly creating it. If you are intentionally doing it, please add a new
keyword to it otherwise change the name of the derived class method if it is created mistakenly.
In c#, the functionality of method hiding and method overriding will be same. The only difference between Method Hiding/shadowing and Method Overriding is while accessing the purely defined members using a child class instance.
As discussed, purely defined members mean the members that are re-implemented inside the child class using the hiding approach.
Following is the example of implementing method hiding and method overriding in c#.
In this example, we created a base class (Vehicle) variable (carVehicle) using a child class (Car) instance and tried to access the required methods.
When you execute the above example, you will get the following result.
If you observe the above result, the GetDetails() method is executed from the child class instance but the SetDetails() method is executed from the parent class because the overriding method (GetDetails) is not considered as a pure child class member due to that it executed the GetDetails() from child instance. The SetDetails() method in the child class is implemented using the method hiding approach due to that it is considered as a pure child class member and it is not accessible by the parent reference variable.
In c#, while working with polymorphism the method hiding will make code harder to understand and maintain. Instead of method hiding, it is always advisable to use method overriding because it will the derived class to replace the base class method implementation.