Method Hiding In C#
In this article, I assume you already know about Inheritance. If you have been following this series, you must have read our article on Inheritance. Now let's discuss what is Method Hiding In C#.
What if our Derived Class defines a method with the same signature that already exists in our Base Class? This is Method Hiding. By doing this, derived class hides the method provided by the base class. Refer below code snippet -
We have a base class named as Car with a method drive(). Now we define a new class named MyCar that inherits from Car class. This MyCar class also defines a method which is exactly similar to drive() method of base class. Doing this does not generate any error but compiler warns us that doing this will hide the implementation provided by the base class. If you really want to do this, then use "new" keyword. This is the exact warning - 'MyCar.drive()' hides inherited member 'Car.drive()'. Use the new keyword if hiding was intended.
What if I still want to access the base method? We can access the base method using the base keyword. Here base keyword is used to access hidden members. There are scenarios where we want to execute the base method first and then just to change what we need we just write that portion to update the object state. Refer below code snippet for the use of base keyword -
In the above code snippet, base.drive() method is called to call base class's drive method. This is how we call hidden members of the base class. base keyword refers to the base class object and for MyCar base class is Car.
You can create the object of MyCar class and assign it to the reference of Car. i.e. Car o = new MyCar(); Now when you call o.drive() - this will call Car's drive method. Refer below image -
Car o1 = new MyCar(); // Valid Statement (✔)
MyCar o2 = new Car(); // Invalid Statement (❌)
Let us know in comments below if you have any query or concern. We are happy to help. Happy Reading (refer full series here - C# Tutorial Index)
What if our Derived Class defines a method with the same signature that already exists in our Base Class? This is Method Hiding. By doing this, derived class hides the method provided by the base class. Refer below code snippet -
METHOD HIDING IN C#
We have a base class named as Car with a method drive(). Now we define a new class named MyCar that inherits from Car class. This MyCar class also defines a method which is exactly similar to drive() method of base class. Doing this does not generate any error but compiler warns us that doing this will hide the implementation provided by the base class. If you really want to do this, then use "new" keyword. This is the exact warning - 'MyCar.drive()' hides inherited member 'Car.drive()'. Use the new keyword if hiding was intended.
Why would I do this?
There are scenarios where we do not want to use the implementation provided by the parent class and we cannot make any changes to the parent class code. We want some of the functions to behave differently based on our needs. So we use Method Hiding. We just define the new implementation of it in our derived class by using the new keyword. There are other ways as well like Method Overriding which we will see in our next article.
Therefore we do Method Hiding as shown below - (check how new keyword is used)
class Car { public void drive() { Console.WriteLine("Base Class : Drive Method"); } } class MyCar : Car { public new void drive() // Using new keyword here { Console.WriteLine("Derived Class : Drive Method"); } } class Program { static void Main(string[] args) { Car o1 = new Car(); o1.drive(); // Output - Base Class : Drive Method MyCar o2 = new MyCar(); o2.drive(); // Output - Derived Class : Drive Method Console.ReadKey(); } }
Use Of Base Keyword
What if I still want to access the base method? We can access the base method using the base keyword. Here base keyword is used to access hidden members. There are scenarios where we want to execute the base method first and then just to change what we need we just write that portion to update the object state. Refer below code snippet for the use of base keyword -
class Car { public void drive() { Console.WriteLine("Base Class : Drive Method"); } } class MyCar : Car { public new void drive() { base.drive(); //Calling Car's drive() method Console.WriteLine("Derived Class : Drive Method"); } } class Program { static void Main(string[] args) { MyCar o2 = new MyCar(); o2.drive(); //Output //Base Class : Drive Method //Derived Class : Drive Method Console.ReadKey(); } }
In the above code snippet, base.drive() method is called to call base class's drive method. This is how we call hidden members of the base class. base keyword refers to the base class object and for MyCar base class is Car.
Wait, There Is One More Way To Call Hidden Methods -
You can create the object of MyCar class and assign it to the reference of Car. i.e. Car o = new MyCar(); Now when you call o.drive() - this will call Car's drive method. Refer below image -
Parent can hold the reference to its child but Child cannot hold the reference to its parent.
Car o1 = new MyCar(); // Valid Statement (✔)
MyCar o2 = new Car(); // Invalid Statement (❌)
Let us know in comments below if you have any query or concern. We are happy to help. Happy Reading (refer full series here - C# Tutorial Index)
I have one doubt that if we are holding object of derived class into base class reference variable and then calling the method why it calls the base class method why not derived class method is being called?
ReplyDelete