Skip to content
LLD Learn/OOP Foundations & UML
Browsing as a guest. Sign in to save your progress and earn XP as you complete chapters.

Interfaces, Abstract Classes & Contracts

4 min read

You'll learn to

  • -Explain an interface as a contract and an abstract class as a contract plus some shared parts
  • -Choose between them by asking whether the implementations share real state or code
  • -Type a variable as the interface so a new implementation, or a test fake, can be swapped in

Tip: the highlighter is on - just select any text below to mark it. Use the highlighter button up top to change color or turn it off. Highlights are saved on this device only. Log in to save them to your account instead.

Interfaces and abstract classes both let you define a contract that several classes can satisfy, and candidates often blur them together. A contract here is a list of methods that callers may rely on and that every implementation promises to provide. The difference that matters: an interface is a pure contract with no shared code, while an abstract class is a contract plus some state or ready-made behavior that subclasses inherit.

An Analogy: The Wall Socket and the Half-Built Kit

An interface is a wall socket. It only says "anything with this plug shape gets power". The socket does not care whether you plug in a lamp or a toaster, and the lamp and toaster share nothing else. An abstract class is a flat-pack furniture kit with some parts already assembled. Everyone who builds it gets those parts for free, but the kit says clearly that you must still add the missing piece before it counts as furniture.

Python has no interface keyword. The common approach is an abstract base class (ABC) whose methods are all marked @abstractmethod, which means "subclasses must fill this in".

Choosing Between Them

  • -Use an interface when unrelated classes must satisfy the same contract but share no code. For example, a PaymentStrategy that CreditCard, PayPal and Wallet each implement their own way.
  • -Use an abstract class when subclasses share real state or behavior as well as the contract. For example, an abstract Shape that stores a color and offers a ready-made describe(), while leaving area() for each shape to supply.
  • -Most languages let a class implement many interfaces but extend only one class, so interfaces combine more freely.

Run this demo. Shape is the half-built kit: it already stores a color and describes itself, but each shape must supply its own area. Trying to build a plain Shape fails, because the kit is unfinished.

An abstract class carries shared parts, and refuses to be built until the gap is filled

Designing to the Interface, Not the Implementation

The habit that pays off in an interview is to have calling code depend on the interface type, never the concrete class. A NotificationService should hold its sender typed as MessageSender, not as EmailSender, even if email is the only sender today. That one habit is what lets you swap in an SMS sender later, or a fake sender in a test, without touching the service.

Depending on the contract, not the concrete class
from abc import ABC, abstractmethod

class MessageSender(ABC):
    @abstractmethod
    def send(self, message: str) -> None: ...

class EmailSender(MessageSender):
    def send(self, message: str) -> None:
        print(f"Emailing: {message}")

class NotificationService:
    def __init__(self, sender: MessageSender):  # depends on the interface
        self._sender = sender

    def notify(self, message: str) -> None:
        self._sender.send(message)

The test fake is where this habit shows its value in practice. A fake sender that simply records messages lets you test NotificationService without sending a single real email.

A quick tell on a whiteboard: if two classes need the same contract but nothing else in common, reach for an interface. If they would end up duplicating the same field or the same method body, that shared piece belongs in an abstract class.

Interview Signal

You're designing a PaymentStrategy contract used by CreditCard, PayPal, and Wallet, which share no common state or logic. Interface or abstract class?

Weak Answer

"Abstract class, since that's what I'm more used to writing."

Strong Answer

"Interface - these three implementations don't share any state or behavior, they just need to satisfy the same pay() contract differently. An abstract class would either force empty shared state or trick me into adding coupling that isn't actually there. If they later shared real logic, I'd revisit that."

Check Yourself1 / 5

What is the core difference between an interface and an abstract class?

Discussion0

Join the Discussion

Sign in to leave comments, reply to others, or like insights.

Sign In to ScaleDojo

No comments yet. Be the first to start the thread!