In this tutorial, i am going to explain you about the concept of Method Overriding and Method Hiding in Polymorphism. As we had already discussed Polymorphism in our previous session. Let’s elaborate the concept of Method Overriding and Method Hiding in Detail.

At the end of this tutorial, you will be able to understand:

  • Method Overriding and Method Hiding in C#.
  • Use of the virtual, override and new keyword in C#.
  • The example of the virtual, overriding and new keyword in C#.

Introduction:

Method Overriding is a concept of Polymorphism. In Object Oriented Programming a virtual method is a method that can be changed using override keyword in a derived class. The virtual Keyword is used to mark a method. There is one more keyword related to polymorphism is new which is used to hide method of a base class in a derived class.

Method Overriding

The Virtual keyword:

We can mark a method in a base class as virtual. Which specifies that this method can be overridden in the derived class. The Virtual keyword is replaced by override keyword in the derived class.

The Override keyword:

With override keyword, we can modify the functionality of the base class method which is declared as virtual in the base class. We can only override the method in the derived class that is declared as virtual in the base class.

The New Keyword:

Polymorphism also has a concept of method hiding that can be achieved using new keyword in C#. With new keyword, we can hide a method in the base class from derived class.

Let’s understand each keyword through example.

The example of Method Overriding (virtual and overriding keyword):

Open Visual Studio and goto File>New>Project and create a C# Console Application project called MethodOverridingExample,

Write the following code in the Program,cs file.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MethodOverridingExample
{
 class Program
 {
 class AccountBase
 {
 public AccountBase()
 {
 Console.WriteLine("Base Class Constructor");
 }
 public virtual void CalculateInterest()
 {
 double dAmount = 2000;
 double dInterestPercent = 20;
 double dInterestAmount = dAmount * dInterestPercent / 100;
 double dGrossAmount = dAmount + dInterestAmount;
 Console.WriteLine("Base Gross Amount is :{0}", dGrossAmount);

}
 }
 class AccountDerived : AccountBase
 {
 public AccountDerived()
 {
 Console.WriteLine("Derived Class Constructor");

}
 public override void CalculateInterest()
 {

double dAmount = 2000;
 double dInterestPercent = 10;
 double dInterestAmount = dAmount * dInterestPercent / 100;
 double dGrossAmount = dAmount + dInterestAmount;
 Console.WriteLine("Derived Gross Amount is :{0}", dGrossAmount);
 }
 
 }
 static void Main(string[] args)
 {
 AccountBase obj1 = new AccountBase();
 obj1.CalculateInterest();
 AccountDerived obj2 = new AccountDerived();
 obj2.CalculateInterest();
 AccountBase obj3 = new AccountDerived();
 obj3.CalculateInterest();
 Console.ReadKey();
 }
 }
}

Run the application and look at the output screen.

Method Overriding

You can see in the output screen the CalculateInterest function in the base class is overridden by the CalculatedInterest function in the derived class at this line of code.

AccountDerived obj2 = new AccountDerived();
 obj2.CalculateInterest();
 AccountBase obj3 = new AccountDerived();
 obj3.CalculateInterest();

The example of Method Hiding (new keyword):

Open Visual Studio and goto File>New>Project and create a C# Console Application project called MethodHidingDemo,

Write the following code in the Program,cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MethodHidingDemo
{
 class AccountBase
 {
 public AccountBase()
 {
 Console.WriteLine("Base Class Constructor");
 }
 public void CalculateInterest()
 {
 double dAmount = 2000;
 double dInterestPercent = 20;
 double dInterestAmount = dAmount * dInterestPercent / 100;
 double dGrossAmount = dAmount + dInterestAmount;
 Console.WriteLine("Base Gross Amount is :{0}", dGrossAmount);

}
 }
 class AccountDerived : AccountBase
 {
 public AccountDerived()
 {
 Console.WriteLine("Derived Class Constructor");

}
 public new void CalculateInterest()
 {

double dAmount = 2000;
 double dInterestPercent = 10;
 double dInterestAmount = dAmount * dInterestPercent / 100;
 double dGrossAmount = dAmount + dInterestAmount;
 Console.WriteLine("Derived Gross Amount is :{0}", dGrossAmount);
 }

}
 class Program
 {

static void Main(string[] args)
 {
 AccountBase obj1 = new AccountBase();
 obj1.CalculateInterest();
 AccountDerived obj2 = new AccountDerived();
 obj2.CalculateInterest();
 AccountBase obj3 = new AccountDerived();
 obj3.CalculateInterest();
 Console.ReadKey();
 }
 }
}

Run the application and look at the output screen.

Method Overriding

You can compare the output window of both examples.  At the following line of code, the base class CalculateInterest method is hidden in derived class

 AccountBase obj3 = new AccountDerived();
 obj3.CalculateInterest();

Conclusion:

Hope you understand the concept of Method Overriding and Method Hiding in Polymorphism. We will create more examples on these topics in later sessions.

View More:

Thank You.

 

Leave a Comment