Search This Blog

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 -

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)

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 -

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.

Post a Comment

Popular posts from this blog

Great android apps require great design. For great design you need to master layouts. Layout is one of the basic building block of Android User Interface. Lets jump right in and try designing layout for our app - Just Maths.

Just Maths has 3 screens - Main Menu Screen, Game Screen and Score Screen. Each of the screen is designed using Relative Layout and Linear Layout. There are other layouts as well like Frame Layout, Table Layout and Grid Layout. But you can design anything using only Relative Layout and Linear Layout. These layouts are nothing but View Groups.

Want to build Android Apps really fast?
With Android Studio and a blend of Java and XML, you will be up and running and coding Great Android Apps in no time.
Let's get started. We will be creating a very simple app named Just Maths that will ask user a Maths question. User either presses (✔) or (X) to answer the question. This is going to be a project based learning wherein you will have something concrete in the end.

Here's is a quick way of implementing Auto-Complete in your webpage. Auto-Complete dropdowns are some textbox type looking elements on webpage. When you type on these textboxes you will be prompted with value options just like in dropdowns. The good part of using this method is you need not to using any jQuery for this. The bad part is this will only work in modern browsers which support HTML5.