DEV Community

Khafido Ilzam
Khafido Ilzam

Posted on

How do Interfaces connect with OOP and SOLID?

First, what’s an Interface again?

An interface defines a contract — a list of methods or properties that a class must implement.
It tells you what should exist, but not how it works.

Now, let’s see how interfaces relate to the 4 pillars of OOP.

Abstraction

Interfaces are a tool to achieve abstraction.

  • Abstraction means showing only what’s necessary and hiding the details.
  • An interface only describes behavior (what methods exist) — not how they’re done. So, interface = pure abstraction layer.

Encapsulation

Encapsulation is about hiding internal data and logic, while interfaces define public behavior.

  • The interface tells others what’s accessible publicly (the methods).
  • The actual implementation inside the class (private variables, logic) remains encapsulated. The interface exposes what can be done, not how it’s done. Encapsulation keeps the hidden details safe inside.

Inheritance

While inheritance lets a class inherit code and behavior,
Interfaces let a class promise to follow a certain shape or behavior.
You can also extend interfaces — similar to class inheritance.

This allows structured hierarchies without code duplication — similar to inheritance but purely behavioral.

Polymorphism

Interfaces are the foundation of polymorphism.

Polymorphism allows you to treat different objects in the same way if they share a common interface.


Interfaces are the backbone of Abstraction and Polymorphism,
while also supporting Encapsulation and flexible Inheritance.

They define how objects should look — not how they work.

Interfaces are at the core of two SOLID principles (ISP & DSP):

I — Interface Segregation

Clients should not be forced to depend on interfaces they do not use.
In simple terms, don’t make a big “God interface” that includes methods unrelated to certain classes.
Instead, split it into smaller, more specific interfaces.

  • OOP Connection: Interfaces define abstraction.
  • ISP ensures that abstraction stays clean and specific.
  • Each interface represents a single responsibility for behavior.

D - Dependency Inversion Principle (DIP)

High-level modules should not depend on low-level modules. Both should depend on abstractions (interfaces).

Don’t make your main logic depend on specific classes — depend on interfaces instead.

This allows you to swap implementations easily (for example, switching from MySQL to MongoDB) without breaking your main business logic.

  • OOP Connection: Interface provides abstraction between classes.
  • DIP enforces that both high-level and low-level modules depend on that abstraction, not on each other directly.
  • Promotes loose coupling, flexibility, and testability.

Top comments (0)