Intermediate 25 min read

Abstract Classes

By the end of this lesson, you'll be able to:

  • Explain what an abstract class is and why it can't be instantiated directly
  • Write an abstract method that every subclass must implement
  • Distinguish an abstract class from a regular virtual class

Prerequisites: "Virtual and Override"

A class you can never create directly

public abstract class Shape {
    public abstract Decimal calculateArea();
}

// Shape s = new Shape(); // compile error — abstract classes cannot be instantiated
  1. public abstract class Shape { — abstract means this class can never be created directly.
  2. public abstract Decimal calculateArea(); — an abstract method: a signature with no { } body at all — "calculate the area of a generic shape" doesn't mean anything until you know what kind of shape it is.
  3. } — closes the class.
  4. (blank line)
  5. // Shape s = new Shape(); — commented out because this line would not compile — abstract classes cannot be instantiated.

abstract methods have no body — subclasses must supply one

public class Circle extends Shape {
    public Decimal radius;

    public override Decimal calculateArea() {
        return Math.PI * radius * radius;
    }
}

public class Rectangle extends Shape {
    public Decimal width;
    public Decimal height;

    public override Decimal calculateArea() {
        return width * height;
    }
}
  1. public class Circle extends Shape { — a concrete subclass.
  2. public Decimal radius; — its own field.
  3. (blank line)
  4. public override Decimal calculateArea() { — required — any class that extends Shape must provide its own calculateArea(), or it fails to compile.
  5. return Math.PI * radius * radius; — the circle-specific formula.
  6. } — closes the method.
  7. } — closes Circle.
  8. (blank line)
  9. public class Rectangle extends Shape { — a second concrete subclass.
  10. public Decimal width; — a field.
  11. public Decimal height; — another field.
  12. (blank line)
  13. public override Decimal calculateArea() { — its own required implementation.
  14. return width * height; — a completely different formula from Circle's.
  15. } — closes the method.
  16. } — closes Rectangle.

Abstract class vs regular virtual class

A virtual class can be instantiated on its own and may have methods a subclass chooses to override. An abstract class cannot be instantiated on its own, and any abstract methods it declares must be overridden by every subclass. Reach for abstract specifically when the parent's version genuinely shouldn't exist standalone.

Exercise

Write an abstract class PaymentMethod with an abstract method Decimal processFee(Decimal amount). Write a subclass CreditCard that implements processFee as amount * 0.03.

Show hint

public abstract Decimal processFee(Decimal amount); then override it in CreditCard.

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Abstract Classes Quiz

1. Can you write new Shape() directly if Shape is an abstract class?

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

An abstract class defines a shared shape that every subclass must follow but can never be created directly itself — useful when "generic version" genuinely makes no sense on its own.