CodeBlanks Logo Icon CodeBlanks

virtual vs abstract

C# course · Intermediate · Lesson 55/116

Learn the difference between virtual and abstract methods.

virtual vs abstract

Both virtual and abstract methods can be overridden in a subclass; but there are key differences. A virtual method has a body and override is optional. An abstract method has no body and override is required.

Virtual says "there is a default behavior but it can be changed". Abstract says "the subclass must provide this behavior". Virtual does not need to be in an abstract class; it can live in any class. Abstract only lives in an abstract class.

When to use which?

If a method has a sensible default behavior, use virtual. Example: Account.Withdraw decreases the balance by default; a subclass can change it.

If the subclass must provide its own behavior, use abstract. Example: Shape.Area is computed differently for each shape; so it is abstract. Virtual gives flexibility; abstract gives a strict contract.

Exercise Account class

Add a public virtual void Withdraw(decimal amount) method to Account; its body should do Balance -= amount; and Console.WriteLine("Withdrawn: " + amount);. In SavingsAccount write the public override void Withdraw(decimal amount) body: Balance -= amount; and Console.WriteLine("Savings: " + amount);. In Main, create Account a = new SavingsAccount { Balance = 100 };, call a.Withdraw(20) and print a.Balance.

Show example solution
" + amount); }` to Account. Add `public override void Withdraw(decimal amount) { Balance -= amount; Console.WriteLine("Savings: " + amount); }` to SavingsAccount. In Main write `Account a = new SavingsAccount { Balance = 100 }; a.Withdraw(20); Console.WriteLine(a.Balance);`.
Example solution:
using System;

class Account
{
    public decimal Balance { get; set; }

    public virtual void Withdraw(decimal amount)
    {
        Balance -= amount;
        Console.WriteLine("Withdrawn: " + amount);
    }
}

class SavingsAccount : Account
{
    public override void Withdraw(decimal amount)
    {
        Balance -= amount;
        Console.WriteLine("Savings: " + amount);
    }
}

class Program
{
    static void Main()
    {
        Account a = new SavingsAccount { Balance = 100 };
        a.Withdraw(20);
        Console.WriteLine(a.Balance);
    }
}

Test yourself

  1. Which statement about virtual vs abstract is correct?

    • virtual can have a body, abstract cannot.
    • virtual and abstract are the same thing.
    • An abstract method must also be marked virtual.
    • virtual methods cannot be overridden.
    Show answer

    virtual can have a body, abstract cannot. — Correct. virtual has a body, abstract does not.

  2. Match: virtual vs abstract

    virtual
    has a body
    abstract
    bodyless
    virtual override
    optional
    abstract override
    required
  3. Fill in the blank: virtual method

    public ____ void Withdraw(decimal amount)
    {
        Balance -= amount;
    }
    Show answer
    public virtual void Withdraw(decimal amount)
    {
        Balance -= amount;
    }

All lessons in Intermediate