Sub topic 10
Defining and implementing interfaces
This chapter assumes you can write basic Apex classes, with fields, methods, and constructors. It also assumes you know what access modifiers do. It builds on the class basics from earlier chapters.
What an interface is
An interface is a contract. It lists method signatures but contains no method bodies and no fields. Any class that implements the interface must provide every method it declares.
Use an interface when several classes should be used the same way, but each does the work differently.
Declaring an interface
// Discount.cls
public interface Discount {
Decimal apply(Decimal amount);
}The method has no access modifier and no body. The interface itself is public or global.
Implementing an interface
A class uses the implements keyword. Each method must be public or global, with the same name, parameter types, and return type as the interface.
// PercentDiscount.cls
public class PercentDiscount implements Discount {
private final Decimal percent;
public PercentDiscount(Decimal percent) {
this.percent = percent;
}
public Decimal apply(Decimal amount) {
return amount - (amount * percent / 100);
}
}
// FlatDiscount.cls
public class FlatDiscount implements Discount {
private final Decimal off;
public FlatDiscount(Decimal off) {
this.off = off;
}
public Decimal apply(Decimal amount) {
return amount - off;
}
}Using the interface type
A variable can have the interface type and hold any implementing object. The calling code never names the concrete class.
List<Discount> discounts = new List<Discount>{
new PercentDiscount(10),
new FlatDiscount(5)
};
Decimal price = 100;
for (Discount d : discounts) {
price = d.apply(price);
}
System.debug(price); // 85The first discount takes 100 to 90, and the second takes 90 to 85. To add a new kind of discount, write a new class. The loop does not change.
Extending and combining interfaces
A class can implement more than one interface, separated by commas. An interface can also extend another interface with extends.
public interface Auditable {
String auditLabel();
}
public interface NamedDiscount extends Discount {
String name();
}A class that implements NamedDiscount must provide apply and name.
Common errors
- Giving an interface method an access modifier, such as
public Decimal apply(Decimal amount);. The interface declaration fails to compile. - Implementing a method with no modifier or with
private. The implementing method must bepublicorglobal. - Changing a signature. Returning
Integerinstead ofDecimal, or changing a parameter type, means the method does not satisfy the interface. - Trying
new Discount(). An interface cannot be instantiated. Create an implementing class instead. - Putting a field or constant in an interface. Interfaces declare methods only.
Exercise
A class declares implements Discount but writes Decimal apply(Decimal amount) with no access modifier. Does it compile? Why or why not?
Show answer
No. The implementing method must be public or global. A method with no modifier is private by default, so it cannot satisfy the interface. Adding public fixes it.