DEV Community

sour_grape
sour_grape

Posted on

My Take on OOP (Part 6): Forget "Polymorphism." It's Just Swapping Parts.

I Like Feynman

I'm a big fan of Richard Feynman. If you study physics, you usually end up reading his famous "Red Books," and from there, you get into his biographies. Feynman used to say, "If you can't explain something in simple terms, you don't understand it yourself."

That's probably why he won a Nobel Prize for explaining theories with simple diagrams.

Anyway, my point is, I don't get why people insist on explaining Object-Oriented Programming in such a ridiculously complicated way.

Benefits of an Interface

I've been emphasizing that the biggest benefit of an interface is that it lets you define and enforce a contract in code. Another great benefit is polymorphism.

I already explained this naturally when I talked about abstraction in the last part.

If you just write up the contract, you don't have to care how it's implemented. To bring back the analogy from last time:

The contract "Come by car" just means you have to show up in a car. My friend doesn't care if I take a taxi, a bus, or my own car. I just have to get there by car.

So if you flip that around, it means as long as I'm in a car, I can change my mind whenever I want, right?

I was planning to take the bus, but I'm running late, so I'll take a taxi instead. No problem. This freedom to change my mind based on my situation, to swap things out as I please—that's polymorphism.

Why Polymorphism Exists: OOP and Messages

The core of OOP, as I've said, is a strict division of labor. It's a system where everyone minds their own business and just sends messages to each other. It's a system where you just throw the contract over the wall and tell the other guy, "You figure it out." Polymorphism is the natural result of this system.

My way? Yeah, my way. As long as I honor the contract, I'll do it however I want. And if I don't like it later, I'll swap it out myself. You just get the result. That's the deal. That's the contract.

Implementing, swapping, and changing things however you want—as long as you uphold the contract and deliver the result—that's a successful division of labor, that's successful messaging, that's proper OOP.

The Benefit of Polymorphism

I'll show you exactly what this looks like in code.

// The Contract
interface Car {
    void moveTo(String destination);
}
Enter fullscreen mode Exit fullscreen mode
class Person {
    // anyCar, i.e., as long as it's a car, anything goes.
    public void goToAppointment(Car anyCar) {
        anyCar.moveTo("the meeting spot");
    }
}
Enter fullscreen mode Exit fullscreen mode
public class Main {
    public static void main(String[] args) {
        // "Me," who needs to get to an appointment
        Person me = new Person();

        // I can make and use whatever I want to ride.
        Car myCar = new MyFordMustang();
        Car taxi = new Taxi();
        Car bus = new Bus();

        System.out.println("--- Today's Plan ---");

        // I just have to honor the "Come by car" contract.
        // Which 'Car' I use is completely up to me.

        System.out.println("1. Maybe I'll take my own car today?");
        me.goToAppointment(myCar); // I'm taking my car.

        System.out.println("2. I should take a taxi tomorrow.");
        me.goToAppointment(taxi); // Gotta take a taxi.

        System.out.println("3. Let's take the bus the day after.");
        me.goToAppointment(bus); // Taking the bus.
    }
}
Enter fullscreen mode Exit fullscreen mode

You hear a lot of talk about "flexibility" and "extensibility." Isn't this more intuitive? The contract just has to be met, so of course it's flexible and extensible.

No, let's put it more simply: The biggest advantage is that nothing breaks when I swap things out as I please.

When you want to change code later, it usually falls into two categories:

  1. You change the requirements and the contract.

  2. You change the implementation.

Number 1 means you just change the interface.
Number 2 means you just build a new implementation, throw out the old one, and swap in the new one.

Polymorphism is what gives you the power to do #2.

Conclusion

Polymorphism is born from the division of labor and messaging in OOP.
Polymorphism is the freedom to swap things out as you please, as long as you honor the contract.

Top comments (0)