Abstract Methods in C#

An abstract method is a method without a body. The implementation of an abstract method is done by a derived class. When the derived class inherits the abstract method from the abstract class, it must override the abstract method. When you create a derived class, you must provide an override method for all abstract methods in the abstract class.

 

  1. namespace ConsoleApplication2  
  2. {  
  3.     class Program  
  4.     {  
  5.         public static void Main()  
  6.         {  
  7.   
  8.           ovrCalculate ovr= new ovrCalculate();             
  9.           Console.WriteLine("sum are  "+ovr.Addition(3,3));  
  10.           Console.WriteLine("Multiplication are "+ovr.Multiplication(3, 3));    
  11.           Console.ReadLine();         
  12.         }  
  13.     }  
  14.   
  15.    abstract class Calculate  
  16.    {  
  17.             //A Non abstract method  
  18.   
  19.             public int Addition(int n1, int n2)  
  20.             {               
  21.                 return n1 + n2;  
  22.   
  23.             }  
  24.   
  25.           //An abstract method, to be overridden in derived class  
  26.   
  27.             public abstract int Multiplication(int n1, int n2);  
  28.   
  29. }  
  30.   
  31.    class ovrCalculate : Calculate  
  32.    {  
  33.   
  34. //Here we not implementing abstract methods , or not writing body of abstract class. On //copilation time it raise ab error like  
  35. }  

So, a class derived from an abstract class contains abstract methods and those methods must be defined in the derived class otherwise it won't compile. 
  1. class ovrCalculate : Calculate  
  2.    {  
  3.   
  4.        public override int Multiplication(int Num1, int Num2)  
  5.        {  
  6.   
  7.            return Num1 * Num2;  
  8.   
  9.        }  
  10.    }  
Now it compiles successfully.

And the output is:



By default, methods are non-virtual. You cannot override a non-virtual method.

You cannot use the virtual modifier with the static, abstract, private, or override modifiers. The following example shows a virtual property:
  1. class Program  
  2.     {  
  3.         public static void Main()  
  4.         {  
  5.             Shape ss = new Shape(2.2,2.2);  
  6.             Console.WriteLine("here shape class area method call " + ss.Area());  
  7.             Circle cc = new Circle(2.0);       
  8.             Console.WriteLine("derived class area methods call  "+cc.Area());  
  9.             Console.ReadLine();  
  10.               
  11.         }  
  12.     }  
  13.   
  14.     public class Shape  
  15.     {  
  16.         public const double PI = Math.PI;  
  17.         protected double x, y;  
  18.         public Shape()  
  19.         {  
  20.         }  
  21.         public Shape(double x, double y)  
  22.         {  
  23.             this.x = x;  
  24.             this.y = y;  
  25.         }  
  26.   
  27.         public virtual double Area()  
  28.         {  
  29.             return x * y;  
  30.         }  
  31.     }  
  32.   
  33.     public class Circle : Shape  
  34.     {  
  35.         public Circle(double r)  
  36.             : base(r, 0)  
  37.         {  
  38.         }  
  39.   
  40.         public override double Area()  
  41.         {  
  42.             return PI * x * x;  
  43.         }  
  44.     }  
  45.       
The output would be:



So here I tried to explain abstract methods and the virtual and override keywords.


Similar Articles