Differentiate Hiding And Overriding

Introduction

As most of you know, hiding and overriding are two main features based on 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.

What is the difference between "BaseClass" and "DerivedClass"?

BaseClass" and "DerivedClass" are terms used in object-oriented programming to describe the relationship between two classes. In object-oriented programming, classes are used to define objects and their behavior.The DerivedClass should be a subclass of BaseClass (i.e. should be defined 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 subclass 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 web form and just write the below code in the load event of a form.

private void Page_Load(object sender, System.EventArgs e)
{
    DerivedClass der = new DerivedClass();
    Label1.Text = der.FunctionToOverride();
}

You will see the message from the derived class function FunctionToOverride() as it overrides the base class function FunctionToOverride();. Now you just remove the "virtual" keyword from the function "FunctionToOverride()" of the 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 the point that for overriding to perform keywords like "virtual, abstract, etc., want to be used 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 i.e. we are going to call the hiding function in the derived class.

DerivedClass der = new DerivedClass();
Label1.Text = der.FunctionToHide();

You will, of course, get the message from the hiding function of the derived class. Here hiding is performed by using the keyword new as a modifier in the derived class function. No additional modifiers are needed for the base class. Now you just remove the "new" keyword from the 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 output as just above. So another point we got as if we define 2 exactly similar functions with the same prototype in a base class and derived class, "Hiding" will be the default action performed.

So you listening to the basic differences between hiding and overriding. So here is another one just for you to try. Make the access modifiers of the base class 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 the program won't compile if you change the access modifier of the 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 structure in the base class i.e. class with the 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 the name "DerivedClass".

public struct StructToOverride
{
    public 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 the 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 the 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 is overriding support 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 as 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.


Similar Articles