Abstract Class & Virtual Functions

Abstract and Sealed Keywords

The abstract keyword enables you to create classes and class members that are incomplete and must be implemented in a derived class.

The sealed keyword enables you to prevent the inheritance of a class or certain class members that were previously marked virtual.

Abstract Classes and Class Members

Classes can be declared as abstract by putting the keyword abstract before the class definition.

public abstract class A
{
    // Class members here.
}

An abstract class cannot be instantiated. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.

Abstract classes may also define abstract methods. This is accomplished by adding the keyword abstract before the return type of the method.

public abstract class A
{
    public abstract void DoWork(int i);
}

Note : Abstract methods have no implementation, so the method definition is followed by a semicolon instead of a normal method block. Derived classes of the abstract class must implement all abstract methods. When an abstract class inherits a virtual method from a base class, the abstract class can override the virtual method with an abstract method.

public class D
{
    public virtual void DoWork(int i)
    {
        // Original implementation.
    }
}

public abstract class E : D
{
    public abstract override void DoWork(int i); //Overriding a virtual method
}

public class F : E
{
    public override void DoWork(int i)
    {
        // New implementation.
    }
}

Note : If a virtual method is declared abstract, it is still virtual to any class inheriting from the abstract class. A class inheriting an abstract method cannot access the original implementation of the method—in the previous example, DoWork on class F cannot call DoWork on class D. In this way, an abstract class can force derived classes to provide new method implementations for virtual methods.

Virtual Functions

A virtual method is used to override specified base class implementation when a runtime object is of the derived type. Thus, virtual methods facilitate the consistent functionality of a related object set.

For Eg : An example of a virtual method implementation is classes Manager and Clerk, derived from the base class Employee with a CalculateSalary virtual method, which may be overridden in derived classes with the necessary logic for the appropriate type. A list of Employee type objects may be called at runtime to calculate a salary - without knowing the specific implementation type.

Difference between Virtual Function and Abstract Method

An abstract function cannot have functionality. Any child class MUST give their own version of this method, however it's too general to even try to implement in the parent class.

A virtual function has the functionality that may or may not be good enough for the child class. So if it is good enough, use this method, if not, then override it, and provide your own functionality.