Inheritance
By the end of this lesson, you'll be able to:
- Write a subclass that extends a parent class using extends
- Explain what a subclass automatically gets from its parent
- Recognize inheritance as a way to model an "is-a" relationship
Prerequisites: "Encapsulation"
A parent class and a subclass
public virtual class Vehicle {
public String licensePlate;
public void startEngine() {
System.debug('Engine started.');
}
}
public class DeliveryVan extends Vehicle {
public Decimal cargoCapacityKg;
}
DeliveryVan van = new DeliveryVan();
van.licensePlate = 'CA 123-456'; // inherited from Vehicle
van.startEngine(); // inherited from Vehicle
van.cargoCapacityKg = 1200; // defined on DeliveryVan itself
public virtual class Vehicle {— the parent class.virtualis what allows another class to extend it (covered fully in the next lesson).public String licensePlate;— a field everyVehiclehas.- (blank line)
public void startEngine() {— a method everyVehiclehas.System.debug('Engine started.');— its behavior.}— closes the method.}— closesVehicle.- (blank line)
public class DeliveryVan extends Vehicle {—extends VehiclemeansDeliveryVanautomatically gets everythingVehicledefines.public Decimal cargoCapacityKg;— plus a field of its own.}— closesDeliveryVan.- (blank line)
DeliveryVan van = new DeliveryVan();— creates a van.van.licensePlate = 'CA 123-456';— inherited fromVehicle— never redeclared onDeliveryVan.van.startEngine();— also inherited fromVehicle.van.cargoCapacityKg = 1200;— defined onDeliveryVanitself.
The "is-a" test
Inheritance models a genuine is-a relationship: a DeliveryVan is a Vehicle. If that sentence doesn't sound natural for two classes, they probably shouldn't inherit from each other — a Driver is not a Vehicle, for example, even though a driver operates one. That relationship belongs to a different pattern (composition, covered later in this module).
A real business example: Media (Publishing)
public virtual class Publication {
public String title;
public String getCitation() {
return title;
}
}
public class Magazine extends Publication {
public Integer issueNumber;
}
public class Newspaper extends Publication {
public Date publishDate;
}
public virtual class Publication {— the shared parent.public String title;— a field both subclasses will inherit.- (blank line)
public String getCitation() {— a method both subclasses will inherit.return title;— its behavior.}— closes the method.}— closesPublication.- (blank line)
public class Magazine extends Publication {—Magazineis a publication.public Integer issueNumber;— plus its own specific data.}— closesMagazine.- (blank line)
public class Newspaper extends Publication {—Newspaperis a publication too.public Date publishDate;— with its own specific data.}— closesNewspaper.
Both Magazine and Newspaper genuinely are publications, sharing a title and a citation method, while each adds its own specific data — exactly the shape inheritance is meant for.
Exercise
Write a virtual class Employee with a public String name and a method describe() returning name. Write a subclass Manager extends Employee that adds a public Integer teamSize.
Show hint
public class Manager extends Employee { public Integer teamSize; }
Inheritance 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
Inheritance lets one class (a subclass) automatically get the fields and methods of another (its parent class), then add or change behavior of its own — modeling a genuine "is-a" relationship between things.