Differentiate Hiding and Overriding

As most of you know hiding and overriding are two main features based upon inheritance, which is one of the pillars of the OOP. Using these we can redefine a member of the base class in a derived class. But what are the differences. Let me clear this with a simple example.

Start a new web application project with a default web form named "WebForm1" using visual studio. Add 2 class to this project with names "BaseClass" and "DerivedClass". The "DerivedClass" should be a subclass of "BaseClass" (i.e. should define like DerivedClass: BaseClass).

Define the following functions in the base class named "BaseClass". Notice the keyword "virtual". 

public virtual string FunctionToOverride()

{

          return "This is from base class FunctionToOverride";

}

 

public string FunctionToHide()

{

          return "This is from base class FunctionToHide";

}

 

Now define the following functions in the sub class i.e. in class "DerivedClass". Notice the keyword "new". 

 

public override string FunctionToOverride()

{

return "This is from derived class FunctionToOverride";

}

 

public new string  FunctionToHide()

{

          return "This is from derived class FunctionToHide";

}

 

Paste a label control in the webform and just write below code in the load event of form

 

private void Page_Load(object sender, System.EventArgs e)

{

          DerivedClass der = new DerivedClass();

          Label1.Text =  der.FunctionToOverride();

}

 

You will see the message form derived class function "FunctionToOverride()" as it's overrides the base class function "FunctionToOverride()". Now you just remove the "virtual" key word from the function "FunctionToOverride()" of base class i.e. define it like below

 

public string FunctionToOverride()

{

          return "This is from base class FunctionToOverride";

}

 

If you try to compile it error will generate. So we got a point that for overriding to perform keywords like "virtual", abstract etc... want to be use as modifiers in the base class.

 

So now we are going to test the hide functionality. For that just modify the code in the form load event as follows ie we are going to call the hiding function in derived class. 

 

DerivedClass der = new DerivedClass();

Label1.Text =  der.FunctionToHide();

 

You will of course get the message from the hiding function of derived class. Here hiding is performed by using the key word "new" as a modifier in the derived class function. No additional modifiers needed for base class. Now you just remove the "new" key word from derived class function "FunctionToHide()" as follows.

 

public string  FunctionToHide()

{

          return "This is from derived class FunctionToHide";

}

 

You can still run the program and can produce the same out put as just above. So another point we got as if we define 2 exactly similar functions with same prototype in a base class and derived class, "Hiding" will be the default action perform.

 

So you listening the basic differences of hiding and overriding. So here is another one just for you to try. Make the access modifiers of the base class as protected and keep that of the derived class unchanged. Then you will find that hiding has no problem after changing the base class access modifier. But program won't compile if you change the access modifier of derived class in case of overriding as it won't allow you to change the access modifier of the base class. This is an important point to care about.

 

Let me finish this article by explaining one more point in this matter. Just define the following the structure in the base class i.e. class with name "BaseClass". 

 

public struct StructToOverride

{

          String str;

          public string FunctionInStructure()

          {

                   str = "This is from base class FunctionInStructure";

                   return str;

 

          }

}

 

And add the following structure in the derived class i.e. class with name "DerivedClass".

 

public override struct StructToOverride

{

          String str;

          public string FunctionInStructure()

          {

                   str = "This is from base class FunctionInStructure";

                   return str;

 

          }

}

 

Now just try to compile the program and you will fired with an error specifying that structure is not meant for an override operation.

 

Then what about hiding? Will they support structures? Just check it by first defining the following structure in the base class.

 

public struct StructToHide

{

          String str;

          public string FunctionInStructure()

          {

                   str = "This is from base class FunctionInStructure";

                   return str;

 

          }

}

 

Then the following structure in the derived class with a keyword "new" as below

 

public new struct StructToHide

{

          String str;

          public string FunctionInStructure()

          {

                   str = "This is from derived class FunctionInStructure";

                   return str;

 

          }

}

 

Now try it from the form load event of the web form as below

 

private void Page_Load(object sender, System.EventArgs e)

{                          

WebApplication1.DerivedClass.StructToHide struct1 = new WebApplication1.DerivedClass.StructToHide();

          Label1.Text = struct1.FunctionInStructure();                         

}

 

You will get messages from the hiding structure. So another point as overriding supports methods, properties and some limited class members. But hiding supports more like that of structures.

 

My opinion is that feel free to use hiding as I think it's still not widely used. It is very helpful in case u bought a 3rd party control/dll and you want to use that control without some methods of that control/dll. So just hide them and use other functionalities. Besides you can define your own methods with the same name of those control/dll methods regardless of whether that methods are not defined as virtual/abstract. So let me finish this part and continue your part of finding more.

Up Next
    Ebook Download
    View all
    Learn
    View all