Interfaces vs. Inheritance: Structuring Your Code in OOP

Mohammad Abbas
2 min readNov 5, 2023
Photo by Roman Synkevych on Unsplash

In Object-Oriented Programming (OOP), structuring code can be guided by two core concepts: Interfaces and Inheritance (often via Abstract Classes). Both establish contracts between classes, but they serve different purposes and influence the design of your software in unique ways.

Understanding Interfaces

Interfaces create a contract between different classes, not by defining how something should be done, but by specifying what must be done. They’re like a checklist: “Any class that agrees to this contract must perform these actions.”

Use Cases for Interfaces:

  • When dealing with very different objects: Use interfaces when you have disparate objects that need to collaborate. For instance, different types of payment processors can all adhere to a PaymentProcessor interface.
  • To promote loose coupling: Interfaces help reduce dependencies between the components of a system. Changing the internals of one class won’t affect others that depend on it as long as the interface remains consistent.

Code Example: Implementing an Interface

interface PaymentProcessor {
processPayment(amount: number): void;
}

class CreditCardProcessor implements PaymentProcessor {
processPayment(amount: number) {
// Implementation for credit card processing
}
}
class PayPalProcessor implements PaymentProcessor {
processPayment(amount: number) {
// Implementation for PayPal processing
}
}

In the example above, both CreditCardProcessor and PayPalProcessor are obligated to implement processPayment, but the internal workings can be completely different.

Inheritance and Abstract Classes

Inheritance, often implemented through abstract classes, is about building up from a base definition of an object. It’s akin to saying, “This is what we are,” and then extending and enhancing that definition.

Use Cases for Inheritance:

  • When building up a definition of an object: Use inheritance when you’re crafting a detailed hierarchy or when classes share common characteristics and behavior.
  • To strongly couple classes together: This coupling isn’t always negative. It can be beneficial when changes to the base class should propagate to derived classes.

Code Example: Using Abstract Classes

abstract class Payment {
abstract processPayment(amount: number): void;

refundPayment(amount: number) {
// Common refund logic for all payment types
}
}
class DebitCardPayment extends Payment {
processPayment(amount: number) {
// Specific implementation for debit card
}
}

Here, DebitCardPayment inherits the refund logic from Payment but provides its specific implementation for processing payments.

Conclusion

Choosing between interfaces and inheritance should be dictated by the level of similarity between the objects and how tightly you want them coupled. Interfaces are ideal for ensuring that completely different objects can work together under the same operation set, while inheritance/abstract classes are excellent for creating a shared base of operations and characteristics.

In designing your software, thoughtful consideration of these concepts can lead to a codebase that is both flexible and easy to maintain.

--

--