• Lessons Home
Topics
Community
  1. Lessons
  2. Object-Oriented Apex
  3. Inheritance and Interfaces
  4. Extending Classes

      Extending Classes

      Extending Classes

      Sometimes two classes share most of their data and behavior. A manager is still a staff member, and an express package is still a package. Copying the same fields into both classes means fixing every bug twice. Inheritance lets a new class start from an existing one and add only what is different.

      The extends Keyword

      The class you build on is the parent (or superclass), and the new class is the child (or subclass). In Apex the parent must be marked virtual before anything can extend it:

      public virtual class Vehicle {
      	public Integer wheels;
      
      	public String describe() {
      		return 'A vehicle with ' + wheels + ' wheels';
      	}
      }
      
      public class Truck extends Vehicle {
      	public Integer payload;
      }
      

      A Truck object has everything a Vehicle has, plus its own payload field. You can call truck.describe() and read truck.wheels even though Truck never declares them.

      Calling the Parent Constructor with super

      A child constructor often needs the parent to set up its part of the object first. Call the parent constructor with super(...), and make it the first line:

      public virtual class Vehicle {
      	public Integer wheels;
      
      	public Vehicle(Integer wheels) {
      		this.wheels = wheels;
      	}
      }
      
      public class Truck extends Vehicle {
      	public Integer payload;
      
      	public Truck(Integer wheels, Integer payload) {
      		super(wheels);
      		this.payload = payload;
      	}
      }
      

      super(wheels) runs the Vehicle constructor, then the Truck constructor finishes with its own field.

      A Child Is Also a Parent

      Because a Truck is a Vehicle, you can store it in a Vehicle variable: Vehicle v = new Truck(6, 2000);. Through that variable you can only use what Vehicle declares, so v.wheels works but v.payload does not compile.

      Common Mistakes

      Forgetting virtual on the parent is a compile error. Forgetting the super(...) call is also a compile error when the parent has no constructor without parameters, because Apex cannot build the parent part of the object on its own.

      A Note on the Exercise Editor

      The exercise editor runs your code as one anonymous block, and there every class can already be extended. Writing virtual on a class in the editor is actually a compile error (classes are by default virtual), so the starter code leaves it off and adds a comment showing how the class would look in a real org. Everything else, including extends and super, works exactly the same.

      Why This Matters

      Inheritance is how Salesforce frameworks hand you shared behavior. A trigger handler framework, for example, gives you a base class, and your handler extends it to add only the logic for your object.

      Apex Code Editor
      Sign in to Submit

      Welcome to Lightning Challenge!

      How It Works

      • • Write your solution in the code editor
      • • Connect your Salesforce org to test
      • • Submit to check if your solution passes
      • • Use hints if you get stuck

      Note

      Complete this lesson challenge to earn points and track your progress. The code editor allows you to implement your solution, and the tests will verify if your code meets the requirements.

      Wally Assistant

      Wally can't hear you

      Please sign in to access the AI Assistant

      Sign In