Sub topic 11
Extending classes with virtual, abstract, and override
This chapter assumes you can declare a class with fields, methods, and constructors, and that you know access modifiers such as public and private. It also assumes you have seen interfaces. Here you will build a class hierarchy, where one class reuses and customizes another.
Classes are closed by default
An Apex class cannot be extended unless you allow it. Two keywords allow it:
virtualmarks a class that can be extended and may be instantiated itself.abstractmarks a class that can be extended but cannot be instantiated. It may declare methods with no body.
Methods follow the same rule. A subclass can replace a method only if the parent declares it virtual or abstract. The subclass method must then carry the override keyword.
| Keyword | On a class | On a method |
|---|---|---|
| `virtual` | Can be extended | Has a body and can be overridden |
| `abstract` | Must be extended | Has no body and must be overridden |
| `override` | Not applicable | Replaces a parent's virtual or abstract method |
A worked example
// Shape.cls
public abstract class Shape {
protected String name;
public Shape(String name) {
this.name = name;
}
public abstract Decimal area();
public virtual String describe() {
return name + ' with area ' + area();
}
}
// Rectangle.cls
public class Rectangle extends Shape {
private Decimal width;
private Decimal height;
public Rectangle(Decimal width, Decimal height) {
super('Rectangle');
this.width = width;
this.height = height;
}
public override Decimal area() {
return width * height;
}
public override String describe() {
return super.describe() + ' (' + width + ' x ' + height + ')';
}
}
// Usage
Shape s = new Rectangle(3, 4);
System.debug(s.describe()); // Rectangle with area 12 (3 x 4)Four details matter here:
Shapehas noarea()body, so every concrete subclass must supply one.describe()is virtual, soRectanglemay keep it or replace it.Rectanglereplaces it and callssuper.describe()to reuse the parent text.protectedmakesnamevisible to subclasses but not to unrelated code.- Constructors are not inherited.
Rectanglecallssuper('Rectangle')as its first statement. If the parent has only a constructor with parameters, this call is required.
Dispatch uses the real object
The variable s has type Shape, but the object is a Rectangle. Apex picks the method from the object, so s.describe() runs the Rectangle version. The next chapter builds on this.
Common errors
- Missing
override. Declaring a method with the same signature as a virtual parent method, withoutoverride, fails to compile. - Missing
virtualon the parent method. Addingoverrideto a method that the parent did not markvirtualorabstractalso fails. - Instantiating an abstract class.
new Shape('x')does not compile. - Weaker access. An override cannot be less visible than the method it replaces.
- Extending a plain class.
extendsfails on a class that is neithervirtualnorabstract.
Exercise
A class Base declares public String label() { return 'base'; }. A subclass Child extends Base declares public override String label() { return 'child'; }. Why does this fail to compile, and what are the two changes needed?
Show answer
Base is not virtual or abstract, so it cannot be extended, and label() is not virtual, so it cannot be overridden. Declare the class as public virtual class Base and the method as public virtual String label().