Why use an interface in Apex?

I’m studying for my Platform Dev I exam, and I’ve been reading about interfaces. I understand how they work in abstraction, but what I don’t understand is why you would use them. The documentation says they “provide a layer of abstraction to your code,” but that doesn’t really tell me anything. If you have to define all of the methods in your implementing class anyway, how does an interface make your code any cleaner?

Thanks for your help!

Answer

Lets say you have some classes defined that provide the same method, but aren’t otherwise related. E.g (based off a StackOverflow answer)

interface Flyable {
    void Fly();
}

public class Bird extends Reptile implements Flyable {
    public void Fly() {
        //Flap wings
    }
}

public class Plane extends Machine implements Flyable {
    public void Fly() {
        // throttle, elevators, rudder, ailerons, flaps, ...
    }
}

A Bird and a Plane aren’t related by a common base class, so that can’t inherit the implementation of flight. Nor should they, as their implementations to flight are fairly different. But they can both fly.

The interface allows you to perform a common operation on otherwise unrelated objects. E.g.

List<Flyable> flyingThings = GetBirdInstancesAndPlaneInstancesMixed();
for(Flyable item in flyingThings) {
   item.Fly();
}

This is exactly where it makes your code cleaner. How would you otherwise make this list of unrelated things fly? You would have a List<Object> where you need to figure out the type you are dealing with and try and call the applicable method. Getting the runtime-type of an Object in Apex is a pain and there is no switch statement to jump through all the possible options.

Better to have a common interface and let any class that implements it deal with how it gets done.

Thinking about the benefit of Comparable being an interface rather than an abstract base class is similar. You can’t have multiple inheritance in Apex, so if it was a base class rather than interface, anything that wanted to be Comparable would ultimately need to extend from it. That’s not really what you want, as now all sorts of otherwise unrelated objects are interchangeable at the base level. With Comparable as an interface you can still call compareTo() on anything that implements it to sort it without needing to know anything else about how that object.

Reasons to use an interface:

  • Behavior Contract – A common method is needed on otherwise unrelated objects. The implementation of that method could be significantly different. As such, there is little benefit in inheriting the implementation (behavior).
  • Coupling – If code is only dependent on the interface then it is easier to change the implementation, as there no/fewer references to a specific class. E.g. I can change the way a plane flys without any risk of making birds fall out of the sky.

Attribution
Source : Link , Question Author : womanwhodevs , Answer Author : Community

Leave a Comment