Intermediate 25 min read

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
  1. public virtual class Vehicle { — the parent class. virtual is what allows another class to extend it (covered fully in the next lesson).
  2. public String licensePlate; — a field every Vehicle has.
  3. (blank line)
  4. public void startEngine() { — a method every Vehicle has.
  5. System.debug('Engine started.'); — its behavior.
  6. } — closes the method.
  7. } — closes Vehicle.
  8. (blank line)
  9. public class DeliveryVan extends Vehicle { — extends Vehicle means DeliveryVan automatically gets everything Vehicle defines.
  10. public Decimal cargoCapacityKg; — plus a field of its own.
  11. } — closes DeliveryVan.
  12. (blank line)
  13. DeliveryVan van = new DeliveryVan(); — creates a van.
  14. van.licensePlate = 'CA 123-456'; — inherited from Vehicle — never redeclared on DeliveryVan.
  15. van.startEngine(); — also inherited from Vehicle.
  16. van.cargoCapacityKg = 1200; — defined on DeliveryVan itself.

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;
}
  1. public virtual class Publication { — the shared parent.
  2. public String title; — a field both subclasses will inherit.
  3. (blank line)
  4. public String getCitation() { — a method both subclasses will inherit.
  5. return title; — its behavior.
  6. } — closes the method.
  7. } — closes Publication.
  8. (blank line)
  9. public class Magazine extends Publication { — Magazine is a publication.
  10. public Integer issueNumber; — plus its own specific data.
  11. } — closes Magazine.
  12. (blank line)
  13. public class Newspaper extends Publication { — Newspaper is a publication too.
  14. public Date publishDate; — with its own specific data.
  15. } — closes Newspaper.

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; }

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Inheritance Quiz

1. What relationship does inheritance model?

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

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.