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"
public virtual class Vehicle {— the parent class.public virtual String getDescription() {—virtualopts this specific method into being replaceable by subclasses. Without it,DeliveryVanwould not be allowed to change it at all — Apex methods are non-overridable by default.return 'A generic vehicle';— the default behavior.}— closes the method.}— closesVehicle.- (blank line)
public class DeliveryVan extends Vehicle {— the subclass.public override String getDescription() {—overridereplaces the parent's version with this one.return 'A delivery van';— the new behavior.}— closes the method.}— closesDeliveryVan.- (blank line)
Vehicle van = new DeliveryVan();— aDeliveryVanstored in aVehicle-typed variable.System.debug(van.getDescription());— logs"A delivery van"— the actual object's overridden version runs, not theVehicletype'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)"
public class DeliveryVan extends Vehicle {— the subclass.public override String getDescription() {— the override.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.}— closes the method.}— closesDeliveryVan.- (blank line)
// "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
}
}
public virtual class Equipment {— the parent class.public virtual Decimal getDailyRentalRate() {— a sensible default rate, marked changeable.return 500;— the default value.}— closes the method.}— closesEquipment.- (blank line)
public class Crane extends Equipment {— a specific equipment type.public override Decimal getDailyRentalRate() {— overrides the default.return super.getDailyRentalRate() * 4;— builds on the base rate viasuperrather than duplicating the500, since cranes cost more.}— closes the method.}— closesCrane.
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;
Virtual and Override Quiz
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.