Author : Rahul
Last Modified : 20-Aug-2021
Complexity : Beginner

Abstract and Virtual methods in C#


Abstract and virtual methods are used to override the methods in derived class. In this article we will analyze Abstract methods and Virtual methods in details.

Abstract Methods

Abstract methods does not contains any implementation in the base class and they force the derived classes to override the Abstract method. Abstract methods are declared under abstract class. 
Abstract methods contains the following features:

  1. Abstract methods does not have any method body they contains only method signature.
  2. If we want that the derived class should override the base class method then we will define the method as abstract in base class.
  3. It is necessary to implement Abstract method in derived class.
  4. A Class which contains abstract method are called abstract class. We cannot create the instance of this class.
  5. Abstract modifier is used to create Abstract method.
  6. Abstract modifier can not be used with static or virtual or override modifier.
  7. Abstract methods exists in Abstract class only.

Example

using System;

namespace DemoApplication
{
 /// <summary>
 /// Parent class Shape
 /// </summary>
 public abstract class Shape
 {
   //Variable Declaration
   public int width, height;
   
   /// <summary>
   /// Constructor 
   /// </summary>
   /// <param name="w">width</param>
   /// <param name="h">height</param>
   public Shape(int w = 0, int h = 0)
   {
     width = w;
     height = h;
   }
   
   /// <summary>
   /// Calculate area abstract method
   /// </summary>
   /// <returns>Area of the shape</returns>
   public abstract int area();
 }
 
 /// <summary>
 /// Derived class Rectangle
 /// </summary>
 public class Rectangle : Shape
 {
   /// <summary>
   /// Constructor 
   /// </summary>
   /// <param name="w">width</param>
   /// <param name="h">height</param>
   public Rectangle(int w = 0, int h = 0) : base(w, h)
   {
   }
   
   /// <summary>
   /// Calculate area override method
   /// </summary>
   /// <returns>Area of the Rectangle</returns>
   public override int area()
   {
     return (width * height);
   }
 }
 
 /// <summary>
 /// Drived Class Triangle
 /// </summary>
 public class Triangle : Shape
 {
   /// <summary>
   /// Constructor
   /// </summary>
   /// <param name="w">width</param>
   /// <param name="h">height</param>
   public Triangle(int w = 0, int h = 0) : base(w, h)
   {
   }
   
   /// <summary>
   ///  Calculate area override method
   /// </summary>
   /// <returns>Area of the triangle</returns>
   public override int area()
   {
     return (width * height / 2);
   }
 }
 
 class Program
 {
   static void Main(string[] args)
   {
     //For Rectangle
     Shape rectangle = new Rectangle(10, 10);
     int area_Rectangle = rectangle.area();
     Console.WriteLine($"Area of the Rectangle:{area_Rectangle}");
     
     //For Triangle
     Shape triangle = new Triangle(10, 10);
     int area_Triangle = triangle.area();
     Console.WriteLine($"Area of the Triangle:{area_Triangle}");
     
     Console.Read();
   }
 }
}

Output

Area of the Rectangle:100
Area of the Triangle:50

 

Virtual Methods

Virtual methods contains implementation in the base class and it can exist in the abstract and non-abstract class. They provides the option of overriding the virtual method in derived class.
Virtual methods contains the following features:

  1. Virtual methods have method body.
  2. If you think that the derived class may or may not override the base class method, then we will define the method as virtual in base class.
  3. It is not necessary to implement Virtual method in derived class. If we implement virtual method in derived class, it will replace the original implementation.
  4. A Class which contains virtual method are called base class. We can also create the instance of this class.
  5. Virtual modifier is used to create Virtual method.
  6. Virtual modifier can not be used with static or abstract or override modifier.
  7. Virtual methods exists in Abstract class and Non Abstract class.

Example

using System;

namespace DemoApplication
{
 /// <summary>
 /// Parent class Shape
 /// </summary>
 public class Shape
 {
   //Variable Declaration
   public int width, height;
   
   /// <summary>
   /// Constructor 
   /// </summary>
   /// <param name="w">width</param>
   /// <param name="h">height</param>
   public Shape(int w = 0, int h = 0)
   {
     width = w;
     height = h;
   }
   
   /// <summary>
   /// Calculate area virtual method
   /// </summary>
   /// <returns>Area of the shape</returns>
   public virtual int area()
   {
     return 0;
   }
 }
 
 /// <summary>
 /// Derived class Rectangle
 /// </summary>
 public class Rectangle : Shape
 {
   /// <summary>
   /// Constructor 
   /// </summary>
   /// <param name="w">width</param>
   /// <param name="h">height</param>
   public Rectangle(int w = 0, int h = 0) : base(w, h)
   {
   }
   
   /// <summary>
   /// Calculate area override method
   /// </summary>
   /// <returns>Area of the Rectangle</returns>
   public override int area()
   {
     return (width * height);
   }
 }
 
 /// <summary>
 /// Drived Class Triangle
 /// </summary>
 public class Triangle : Shape
 {
   /// <summary>
   /// Constructor 
   /// </summary>
   /// <param name="w">width</param>
   /// <param name="h">height</param>
   public Triangle(int w = 0, int h = 0) : base(w, h)
   {
   }
   
   /// <summary>
   ///  Calculate area override method
   /// </summary>
   /// <returns>Area of the triangle</returns>
   public override int area()
   {
     return (width * height / 2);
   }
 }
 
 class Program
 {
   static void Main(string[] args)
   {
     //For Rectangle
     Shape rectangle = new Rectangle(10, 10);
     int area_Rectangle = rectangle.area();
     Console.WriteLine($"Area of the Rectangle:{area_Rectangle}");
     
     //For Triangle
     Shape triangle = new Triangle(10, 10);
     int area_Triangle = triangle.area();
     Console.WriteLine($"Area of the Triangle:{area_Triangle}");
     
     Console.Read();
   }
 }
}

Output

Area of the Rectangle:100
Area of the Triangle:50