Intro to Interfaces, Contracts, and Concretions
Reading Only

To have polymorphism and inheritance, you need abstractions. To create abstractions, you need an Apex interface. This is where your Apex code really begins to diverge from what's possible in Flow.

An interface is a fully abstract class. What's a fully abstract class? A class that can't be instantiated. Why would you define a class that can't ever be instantiated? I don't know. I was hoping you knew! Just kidding, you'll figure it out in this lesson. I got you.

In Apex, an interface is a type, not a class. They define method signatures but not method implementations. Recall that a method's signature is its name and parameter list. So, an interface ends up looking something like this:

public interface Customer { Boolean isEligibleForDiscount(); void sendNotification(String message); }

isEligibleForDiscount and sendNotification are considered abstract methods because they've been declared without an implementation.

Concretions

Classes that implement interfaces are called concrete classes. These are subtypes.

is_a_relationship

This represents an "is a" relationship within the class hierarchy. A B2B_Customer is a Customer. Here's the corresponding code:

public class B2B_Customer implements Customer { }

The implements keyword indicates which interface a class is implementing. This defines a class hierarchy between the class and the interface. The subclass, B2B_Customer, promises to provide a concrete implementation for each of the interface's abstract methods.

Contracts

What good is a promise if it's not kept? It's the Apex compiler's job to enforce the promise. The subclass will not compile until all the abstract methods of the interface are implemented. This creates a contract between the subclass and the interface that guarantees, at runtime, that a subclass will have the methods defined in the interface. OOP relies heavily on contracts to design object relationships.

The Customer interface outlines a collection of method signatures that define the rules and specifications of a Customer. This becomes our contract. Congrats, you're a lawyer now.

Side Note: If you've ever heard someone call a piece of code a "framework," it usually entails an interface that requires developers to comply with certain specifications to implement the framework.

public class B2B_Customer implements Customer { }

The code above, as is, won't compile because the contract was violated. The contract says B2B_Customer can't exist unless Boolean isEligibleForDiscount() and void sendNotification(String) are implemented. These errors indicate we've violated the contract:

Class B2B_Customer must implement the method: Boolean Customer.isEligibleForDiscount() 
Class B2B_Customer must implement the method: void Customer.sendNotification(String) 

To fix this, provide a concrete implementation for the interface's abstract methods:

public class B2B_Customer implements Customer { public Boolean isEligibleForDiscount(){ Boolean isEligible = false; // logic for determining discount return isEligible; } public void sendNotification(String message){ // logic for sending a notification } }

Okay, now we can start writing some code.