C# Method Overriding

In the last tutorial, we learned about method overloading, in which we can provide same name to many methods but with different parameters. Now in this tutorial, we will learn about method overriding, it is similar to virtual keyword in C++. Basically, in method overriding, we will be overriding the definition of a method defined in parent class (base class) in derived class (child class) using virtual or override keyword in C#.

So, in method override, you override the definition of a method from base class in child class.

If you are not familier with base class and child class, let's understand it first.

A class can be derived from more than one class or interface, which means that it can inherit data and functions from multiple base classes or interfaces.

So, we can create derived class from base class like this

//base class
public class A {
  
}

//derviced class, or child class
public class B:A{

}

Syntax

<acess-specifier> class <base_class_name> {
//
}

<acess-specifier> class <derived_class_name> : <base_class_name> {
   //
}

Now, you are familier with terms base class and derived class, let's understand method override.

Suppose my base class(A) has method "Display()" and I want to override the defination of Display() inside B ( child class), I can do like below

			
public class Program
{
	public class A
	{
          //base class method, created with keyword 'virtual'
	  public virtual  void Display()
	  {
		 System.Console.WriteLine("A::Display");
	  }
	}

	public class B :A
	{
           // dervied class methodm with new definition using override
	   public override void Display()
	  {
		 System.Console.WriteLine("B::Display");
	  }
	}

	  public static void Main()
	  {
		 A a;
		 a = new A();
		 a.Display();    

		 a = new B();
		 a.Display();    
	  }

	
}

Output:

A::Display
B::Display

As you can see in the above example, we have used two keywords "virtual" and "override", to give new definition to method "Display()" inside derived (child- class) B.

With the help of virtual keyword in base class (A), we are notifying C# Compiler that method "Display()"can be changed in the derived class.

With the help of override keyword in child class (B), we are notifying C# compiler that method "Display()" has different code statements than it was in base class.

So, using virtual and override keyword, we accomplish method overrriding in C#.

Method hiding in C#

Above we saw, how we can achieve method overriding using virtual and override keyword, but in Method hiding we use new keyword to provide new definition to the derived class method of base class.

When using new keyword for method hiding, we don't need to declare 'virtual' to base class method.

Example

			
public class Program
{
	public class A
	{
      //base class method, not using virtual method
	  public void Display()
	  {
		 System.Console.WriteLine("A::Display");
	  }
	}

	public class B :A
	{
           // using 'new' keyword for new definition of Display
	   new public void Display()
	  {
		 System.Console.WriteLine("B::Display");	
	  }
	}

	  public static void Main()
	  {
		 A a;
		 a = new A();
		 a.Display();    

		 B b = new B();
		 b.Display();    
	  }

	
}

Output

A::Display
B::Display

In the above code, as you see we have two classes linked in a parent-child relationship and we have a same name method called "Display" which is declared in a child class with a new keyword. So it means it will hide the "A" class display method when called.


Share Tweet