What is the difference between virtual and abstract functions in C#?

CsharpProgrammingServer Side Programming

Abstract methods do not provide an implementation and they force the derived classes to override the method. It is declared under abstract class. An abstract method only has the method definition

Virtual methods have an implementation, unlike the Abstract method and it can exist in the abstract and non-abstract class. It provides the derived classes with the option of overriding it.

Virtual Functions

The virtual keyword is useful in modifying a method, property, indexer, or event. When you have a function defined in a class that you want to be implemented in an inherited class(es), you use virtual functions. The virtual functions could be implemented differently in different inherited class and the call to these functions will be decided at runtime.

The following is a virtual function −

public virtual int area() { }

Here is an example showing how to work with virtual functions −

Example

 Live Demo

using System;

namespace PolymorphismApplication {
   class Shape {
      protected int width, height;
      public Shape( int a = 0, int b = 0) {
         width = a;
         height = b;
      }
      public virtual int area() {
         Console.WriteLine("Parent class area :");
         return 0;
      }
   }
   class Rectangle: Shape {
      public Rectangle( int a = 0, int b = 0): base(a, b) {
      }
      public override int area () {
         Console.WriteLine("Rectangle class area ");
         return (width * height);
      }
   }
   class Triangle: Shape {
      public Triangle(int a = 0, int b = 0): base(a, b) {
      }
      public override int area() {
         Console.WriteLine("Triangle class area:");
         return (width * height / 2);
      }
   }
   class Caller {
      public void CallArea(Shape sh) {
         int a;
         a = sh.area();
         Console.WriteLine("Area: {0}", a);
      }
   }
   class Tester {
      static void Main(string[] args) {
         Caller c = new Caller();
         Rectangle r = new Rectangle(10, 7);
         Triangle t = new Triangle(10, 5);

         c.CallArea(r);
         c.CallArea(t);
         Console.ReadKey();
      }
   }
}

Output

Rectangle class area
Area: 70
Triangle class area:
Area: 25

Abstract Functions

The abstract keyword in C# is used for abstract classes and abstract functions. An abstract class in C# includes abstract and non-abstract methods.

The following is an example of abstract functions in an abstract class in C# −

Example

 Live Demo

using System;
public abstract class Vehicle {
   public abstract void display();
}
public class Bus : Vehicle {
   public override void display() {
      Console.WriteLine("Bus");
   }
}
public class Car : Vehicle {
   public override void display() {
      Console.WriteLine("Car");
   }
}
public class Motorcycle : Vehicle {
   public override void display() {
      Console.WriteLine("Motorcycle");
   }
}
public class MyClass {
   public static void Main() {
      Vehicle v;
      v = new Bus();
      v.display();
      v = new Car();
      v.display();
         v = new Motorcycle();
      v.display();
   }
}

Output

Bus
Car
Motorcycle
raja
Published on 31-Aug-2018 13:55:19
Advertisements