Intermediate 25 min read

Virtual and Override

By the end of this lesson, you'll be able to:

  • Mark a parent class method as virtual to allow it to be changed
  • Use override in a subclass to replace inherited behavior
  • Call the parent's original version from inside an override using super

Prerequisites: "Inheritance"

Making a method changeable with virtual

public virtual class Vehicle {
    public virtual String getDescription() {
        return 'A generic vehicle';
    }
}

public class DeliveryVan extends Vehicle {
    public override String getDescription() {
        return 'A delivery van';
    }
}

Vehicle van = new DeliveryVan();
System.debug(van.getDescription()); // "A delivery van"
  1. public virtual class Vehicle { — the parent class.
  2. public virtual String getDescription() { — virtual opts this specific method into being replaceable by subclasses. Without it, DeliveryVan would not be allowed to change it at all — Apex methods are non-overridable by default.
  3. return 'A generic vehicle'; — the default behavior.
  4. } — closes the method.
  5. } — closes Vehicle.
  6. (blank line)
  7. public class DeliveryVan extends Vehicle { — the subclass.
  8. public override String getDescription() { — override replaces the parent's version with this one.
  9. return 'A delivery van'; — the new behavior.
  10. } — closes the method.
  11. } — closes DeliveryVan.
  12. (blank line)
  13. Vehicle van = new DeliveryVan(); — a DeliveryVan stored in a Vehicle-typed variable.
  14. System.debug(van.getDescription()); — logs "A delivery van" — the actual object's overridden version runs, not the Vehicle type's original.

Calling the original with super

public class DeliveryVan extends Vehicle {
    public override String getDescription() {
        return super.getDescription() + ' (delivery configuration)';
    }
}

// "A generic vehicle (delivery configuration)"
  1. public class DeliveryVan extends Vehicle { — the subclass.
  2. public override String getDescription() { — the override.
  3. return super.getDescription() + ' (delivery configuration)'; — super.getDescription() calls the parent's original version from inside the override, then builds on it rather than fully replacing it.
  4. } — closes the method.
  5. } — closes DeliveryVan.
  6. (blank line)
  7. // "A generic vehicle (delivery configuration)" — the resulting output: the parent's text plus the addition.

A real business example: Construction (Equipment Tracking)

public virtual class Equipment {
    public virtual Decimal getDailyRentalRate() {
        return 500;
    }
}

public class Crane extends Equipment {
    public override Decimal getDailyRentalRate() {
        return super.getDailyRentalRate() * 4; // cranes cost more
    }
}
  1. public virtual class Equipment { — the parent class.
  2. public virtual Decimal getDailyRentalRate() { — a sensible default rate, marked changeable.
  3. return 500; — the default value.
  4. } — closes the method.
  5. } — closes Equipment.
  6. (blank line)
  7. public class Crane extends Equipment { — a specific equipment type.
  8. public override Decimal getDailyRentalRate() { — overrides the default.
  9. return super.getDailyRentalRate() * 4; — builds on the base rate via super rather than duplicating the 500, since cranes cost more.
  10. } — closes the method.
  11. } — closes Crane.

Exercise

Given a virtual class Employee with a virtual method annualBonus() returning 1000, write a subclass Manager that overrides annualBonus() to return double the parent's value using super.

Show hint

return super.annualBonus() * 2;

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Virtual and Override Quiz

1. What must a parent method be marked as before a subclass can override it?

2. super.methodName() calls the subclass's own version of a method.

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

virtual marks a method as changeable by subclasses; override replaces it with new behavior in a specific subclass — and super still gives access to the original if needed.