Intermediate 30 min read

Interfaces

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

  • Define an interface as a pure contract with no implementation at all
  • Implement an interface in a class using implements
  • Explain the difference between an interface and an abstract class

Prerequisites: "Abstract Classes"

A contract with no body at all

public interface Payable {
    Decimal calculatePayment();
}

public class Contractor implements Payable {
    public Decimal hourlyRate;
    public Integer hoursWorked;

    public Decimal calculatePayment() {
        return hourlyRate * hoursWorked;
    }
}
  1. public interface Payable { — interface declares a pure contract, with no fields, constructors, or method bodies allowed.
  2. Decimal calculatePayment(); — a method signature only — no { } body, and no access modifier needed.
  3. } — closes the interface.
  4. (blank line)
  5. public class Contractor implements Payable { — implements Payable promises this class has a working calculatePayment().
  6. public Decimal hourlyRate; — a field specific to Contractor — the interface never mentions it.
  7. public Integer hoursWorked; — another field specific to Contractor.
  8. (blank line)
  9. public Decimal calculatePayment() { — fulfils the promise, with real logic the interface never dictated.
  10. return hourlyRate * hoursWorked; — how this class calculates it — entirely its own choice.
  11. } — closes the method.
  12. } — closes Contractor.

One class, several unrelated interfaces

public interface Payable {
    Decimal calculatePayment();
}

public interface Schedulable_Custom {
    String getSchedule();
}

public class Contractor implements Payable, Schedulable_Custom {
    public Decimal calculatePayment() { return 5000; }
    public String getSchedule() { return 'Mon-Fri, 9-5'; }
}
  1. public interface Payable { — the first contract.
  2. Decimal calculatePayment(); — its one required method.
  3. } — closes Payable.
  4. (blank line)
  5. public interface Schedulable_Custom { — a second, unrelated contract.
  6. String getSchedule(); — its one required method.
  7. } — closes Schedulable_Custom.
  8. (blank line)
  9. public class Contractor implements Payable, Schedulable_Custom { — unlike extends (only one parent), a class can implements multiple interfaces at once.
  10. public Decimal calculatePayment() { return 5000; } — fulfils Payable.
  11. public String getSchedule() { return 'Mon-Fri, 9-5'; } — fulfils Schedulable_Custom.
  12. } — closes Contractor.

Interfaces describe unrelated capabilities a class can mix and match, not a single "is-a" lineage.

Interface vs abstract class

An abstract class can mix real, shared implementation with abstract methods still to be filled in, and a class can only extend one of them. An interface has zero implementation at all, and a class can implement many of them together. Reach for an interface when you're describing an unrelated capability ("this can be paid," "this can be scheduled") rather than a shared lineage.

Exercise

Write an interface Printable with a method String toPrintFormat(). Implement it in a class Invoice that returns "Invoice #" plus an invoiceNumber field.

Show hint

public interface Printable { String toPrintFormat(); }

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Interfaces Quiz

1. How many interfaces can a single class implement?

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 interface is a pure contract — a list of method signatures with zero implementation — that any class can promise to fulfil using implements, regardless of what it inherits from.