What is the difference in Virtual Method and Abstract Method

In C#, an abstract class is a class that cannot be instantiated, and may contain abstract methods. An abstract method is a method that has no implementation in the abstract class and must be implemented in a derived class.

Here are the differences between virtual and abstract methods in an abstract class:

Implementation:
A virtual method has an implementation in the base class and can be overridden in a derived class using the override keyword. An abstract method, on the other hand, has no implementation in the base class and must be implemented in a derived class using the override keyword.

Body:
A virtual method has a body in the base class, and the derived class can choose to either use or override the implementation of the base class. An abstract method has no implementation in the base class and the derived class must provide its own implementation.

Modifier:
A virtual method is marked with the virtual keyword, while an abstract method is marked with the abstract keyword.

Here's an example that demonstrates the differences between virtual and abstract methods in an abstract class:

abstract class Shape
{
    // Abstract method
    public abstract double GetArea();

    // Virtual method with implementation
    public virtual string GetDescription()
    {
        return "This is a shape.";
    }
}

class Circle : Shape
{
    private double radius;

    public Circle(double radius)
    {
        this.radius = radius;
    }

    // Implementing the abstract method
    public override double GetArea()
    {
        return Math.PI * radius * radius;
    }

    // Overriding the virtual method
    public override string GetDescription()
    {
        return $"This is a circle with radius {radius}.";
    }
}

In the above example, Shape is an abstract class that contains an abstract method GetArea() and a virtual method GetDescription(). The Circle class derives from the Shape class and implements the GetArea() method, while also overriding the GetDescription() method.

In summary, the main differences between virtual and abstract methods in an abstract class are that a virtual method has an implementation in the base class and can be overridden in the derived class, while an abstract method has no implementation in the base class and must be implemented in the derived class.