Difference between Virtual and Abstract Keywords in C#

In this article we will explain the some important Difference between Virtual and Abstract Keywords in C#.
  • 13657

Introduction

Abstract and virtual method are used to allow a derived class to override a method of the base class but have some difference, In this article we will explain the some important Difference between Virtual and Abstract Keywords in C#.

Abstract

  • An abstract method has no implementation and its derived class has to implement it.
  • Abstract methods have only the signature. It cannot have method body.
  • If you want to enforce that derived class must override the base class method then you will define the base class method as abstract.
  • Abstract methods should compulsorily be overridden by the derived class.
  • Class containing abstract method cannot be instantiated. It can only be inherited.
  • Abstract modifiers also have a restriction. They cannot be used along with static or virtual or override modifiers.

 Example


using System;

namespace ProgramCall

{

 //Abstract class

    abstract class Shape1

    {

       protected float R, L, B;

        //Abstract methods can have only declarations

       //cannot have method body

        public abstract float Area();

        public abstract float Circumference();

    }

   class Rectangle1 : Shape1

    {

        public void GetLB()

        {

            Console.Write("Enter  Length  :  ");

 

            L = float.Parse(Console.ReadLine());

            Console.Write("Enter Breadth : ");

            B = float.Parse(Console.ReadLine());

        }

       // must be override all the abstract method

      public override float Area()

        {

            return L * B;

        }

     public override float Circumference()

        {

            return 2 * (L + B);

        }

    }

     class MainClass

    {

        // Class containing abstract method cannot be instantiated

        public static void Calculate(Shape1 S)

        {

            Console.WriteLine("Area : {0}", S.Area());

            Console.WriteLine("Circumference : {0}", S.Circumference());

        }

        static void Main()

        {

            Rectangle1 R = new Rectangle1();

            R.GetLB();

            Calculate(R);

            Console.WriteLine();

            Console.Read();

 

        }

    }

}

 

The output of following program


Clipboard171.jpg 

virtual

  • A virtual method has an implementation and its derived class does not have to implement it again (but can replace the original implementation).
  • Virtual method can have a method body.
  • If you feel that the derived class may or may not override the base class method, then you will define the base class method as virtual.
  • Virtual methods need not be compulsorily overridden.
  • Class containing virtual method can be instantiated.
  • There is a restriction when using virtual modifer. You cannot use this modifer along with static or abstract or override modifiers.
Example

using System;

namespace ProgramCall

{

    class Shape

    {

        protected float R, L, B;

        //A virtual method has an implementation

        //Virtual method can have a method body

        public virtual float Area()

        {

            return 3.14F * R * R;

        }

        public virtual float Circumference()

        {

            return 2 * 3.14F * R;

        }

        class Rectangle : Shape

        {

            public void GetLB()

            {

                Console.Write("Enter  Length  :  ");

                L = float.Parse(Console.ReadLine());

                Console.Write("Enter Breadth : ");

                B = float.Parse(Console.ReadLine());

            }

            public override float Area()

            {

                return L * B;

            }

            public override float Circumference()

            {

                return 2 * (L + B);

            }

            static void Main()

            {

                //Class containing virtual method can be instantiated

                Shape s = new Shape();

                s.Area();

                Rectangle R = new Rectangle();

                R.GetLB();

                Console.WriteLine("Area : {0}", R.Area());

                Console.WriteLine("Circumference : {0}", R.Circumference());

                Console.Read();

            }

        }

    }

}

 

The output of the following program


Clipboard171.jpg

© 2019 DotNetHeaven. All rights reserved.