Difference between Virtual and Abstract Method

Key Difference: Abstract methods are the methods that are declared but do not have any implementation. Virtual methods are used for an implementation of the type-based polymorphism. The derived class has the flexibility of re-implementing the virtual method of the base class by using the keyword ‘override’.

In C#, the abstract keyword can be used with both classes and methods. Abstract methods are those methods which lack any form of implementation. However, the implementation logic is obtained by the non-abstract classes derived from them. This is achieved by overriding that method. Due to the absence of actual implementation, the body of the method remains empty. It is important to note that the declaration of an abstract method is only possible in an abstract class. The classes that cannot be initialized are known as abstract classes. A class that inherits from an abstract class must implement all the methods declared as abstract in the abstract class.

For example -

   public abstract class exampleAbstractClass

    {

        public abstract void exampleabstractMethod();

        public virtual void nonAbstractMethod()

        {

            Console.WriteLine("Implementation is defined in the method");

        }

class normalNonAbstractClass : exampleAbstractClass

    {

        public override void exampleabstractMethod ()

        {

            Console.WriteLine("Overriding the exampleabstractMethod of the  class exampleAbstractClass ");

        }

    }

Virtual is also a keyword that is associated with the modification of a method, property, indexer or event declaration. The use of this keyword allows the modification of the declared entity in a derived class. In this example, the derived class has overridden the virtual method declared in the base class.

class Base

{

    public virtual void Test()

    {

          Console.WriteLine("Base.Test");

    }

}

class Derived : Base

{

    public override void Test()

    {

          Console.WriteLine("Derived.Test");

    }

}

Virtual methods are used for an implementation of the type-based polymorphism. The derived class has the flexibility of re-implementing the virtual method of the base class by using the override keyword. Both virtual and abstract cannot be used along with static or virtual or override modifiers.

Comparison between Virtual and Abstract Method:

 

Virtual Method

Abstract Method

Definition

Virtual methods are used for an implementation of the type-based polymorphism.

Abstract methods are the methods that are declared but do not have any implementation. 

Implementation

Yes, can have implementation

No, cannot have any implementation

Need to be overridden

Not compulsory

Compulsory

Keyword used to distinguish it from other types of methods

Virtual

Abstract

Class

Class containing virtual method can be instantiated.

Class containing abstract method cannot be instantiated. It can only be inherited.

Scope

Virtual method's scope to members only.

Abstract method's scope to members and classes.

Method to be executed (object-base class type)

Parent implementation will be called only in the case where no implementation is provided in the concrete class.

Concrete implementation

Method to be executed (object-concrete class type)

Concrete implementation

Concrete implementation

Images Courtesy: aspdotnet-sekhar.blogspot.com

Most Searched in Computers and Internets Most Searched in Games and Recreation
Most Searched in Beauty and Style Most Searched in Pregnancy and Parenting
Hot Chocolate vs Hot Cocoa
Directive vs Regulation
Epistemology vs Ontology

Comments

Good description.Really very nice.

Add new comment

Plain text

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.