Method Overriding Vs Method Hiding in C#
Jul 11, 2015 02:00 0 Comments C# (C-Sharp) Vikram

Method Overriding Vs Method Hiding in C#

 

Method Overriding:

Method Overriding is a technique that allows the invoking of functions from another class (base class) in the derived class. Creating a method in the derived class with the same signature as a method in the base class is called Method Overriding.

In simple words, Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. When a method in a subclass has the same name, same parameters or signature and the same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class.

 

using System;

// Base class

class My_Parent {

                // virtual method

                public virtual void display() {

                                Console.WriteLine("My new parent class. !");  }

}

// Derived class

class My_Child : My_Parent {

                // Here display method is overridden

                public override void display() {

                                Console.WriteLine("My new child class. !"); }

}

class Program {

                public static void Main() {

                                My_Parent obj;

                                // Creating object of the base class

                                obj = new My_Parent();

                                // Invoking method of the base class

                                obj.display();

                                // Creating object of the derived class

                                obj = new My_Child();

                                // Invoking method of derived class

                                obj.display();

                } }

Output:

My new parent class. !

My new child class. !

 

 

 

 

 

Method Hiding:

In Method Hiding, you can hide the implementation of the methods of a base class from the derived class using the new keyword. Or in other words, in method hiding, you can redefine the method of the base class in the derived class by using the new keyword.

 

 

using System;

 

// Base Class

 

public class My_Parent {

  

    public void show()

    {

        Console.WriteLine("This is my parent class.");

    }

}

  

// Derived Class

 

public class My_Child : My_Parent {

  

    // Hide the method of base class using new keyword

    public new void show() {

  

        Console.WriteLine("This is my child class.");

    }

}

  

public class Program {

  

    static public void Main()

    {

  

        // Creating the object of 

        // the derived class

        My_Child obj = new My_Child();

  

        // Access the method of derived class

        obj.show();

    }

}

 

Output:

This is my child class.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Method Overriding Vs Method Hiding

 

METHOD OVERRIDING

METHOD HIDING

In method overriding, you need to define the method of a parent class as a virtual method using virtual keyword and the method of child class as an overridden method using override keyword.

In method hiding, you just simply create a method in a parent class and in child class you need to define that method using new keyword.

It only redefines the implementation of the method.

In method hiding, you can completely redefine the method.

Here overriding is an object type.

Here hiding is a reference type.

If you do not use override keyword, then the compiler will not override the method. Instead of the overriding compiler will hide the method.

If you do not use the new keyword, then the compiler will automatically hide the method of the base class.

In method overriding, when base class reference variable pointing to the object of the derived class, then it will call the overridden method in the derived class.

In the method hiding, when base class reference variable pointing to the object of the derived class, then it will call the hidden method in the base class.

 

 

Prev Next
About the Author
Topic Replies (0)
Leave a Reply
Guest User

You might also like

Not sure what course is right for you?

Choose the right course for you.
Get the help of our experts and find a course that best suits your needs.


Let`s Connect