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. ovrCalculate ovr= new ovrCalculate();
  8. Console.WriteLine("sum are "+ovr.Addition(3,3));
  9. Console.WriteLine("Multiplication are "+ovr.Multiplication(3, 3));
  10. Console.ReadLine();
  11. }
  12. }
  13. abstract class Calculate
  14. {
  15. //A Non abstract method
  16. public int Addition(int n1, int n2)
  17. {
  18. return n1 + n2;
  19. }
  20. //An abstract method, to be overridden in derived class
  21. public abstract int Multiplication(int n1, int n2);
  22. }
  23. class ovrCalculate : Calculate
  24. {
  25. //Here we not implementing abstract methods , or not writing body of abstract class. On //copilation time it raise ab error like
  26. }

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. public override int Multiplication(int Num1, int Num2)
  4. {
  5. return Num1 * Num2;
  6. }
  7. }
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. public class Shape
  13. {
  14. public const double PI = Math.PI;
  15. protected double x, y;
  16. public Shape()
  17. {
  18. }
  19. public Shape(double x, double y)
  20. {
  21. this.x = x;
  22. this.y = y;
  23. }
  24. public virtual double Area()
  25. {
  26. return x * y;
  27. }
  28. }
  29. public class Circle : Shape
  30. {
  31. public Circle(double r)
  32. : base(r, 0)
  33. {
  34. }
  35. public override double Area()
  36. {
  37. return PI * x * x;
  38. }
  39. }
The output would be:



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