Sitemap
DAVID SAMUEL

DAVID SAMUEL

I am proficient in both front-end and back-end development, with expertise in tools like Figma, Framer, and Webflow for creating dynamic designs.

Interfaces and Abstract Classes in Python: Understanding the Differences

4 min readSep 20, 2024

--

Introduction

Interfaces and abstract classes are two important concepts in object-oriented programming. They allow us to write clean, maintainable, and flexible code. In this blog post, we’ll explore the similarities and differences between interfaces and abstract classes in Python, and learn when to use each approach.

What is an interface?

An interface defines a contract between a class and its users. It specifies a set of methods a class must implement to be compatible with the interface. In Python, interfaces can be implemented using abstract base classes (ABCs).

For example, suppose we have a Shape interface that specifies a single method, area(). We can define the Shape interface in Python using the abc module as follows:

from abc import ABC, abstractmethod

class Shape(ABC):
@abstractmethod
def area(self):
pass

Now, any class that implements the Shape interface must provide an implementation of the area() method.

Implementing interfaces in Python

To implement an interface in Python, we create a class that inherits from the interface’s abstract base class. We then provide implementations for all the required methods.

For example, we want to implement the Shape interface for a Rectangle class. We can do so as follows:

class Rectangle:
def __init__(self, width, height):
self.width = width
self.height = height

def area(self):
return self.width * self.height

Notice that we didn’t explicitly inherit from the Shape interface. Instead, we provided an implementation of the area() method, which is all required to be compatible with the Shape interface.

What is an abstract class?

An abstract class is a class that cannot be instantiated directly. Instead, it’s intended to be subclassed by other classes that provide concrete implementations of its abstract methods. Abstract classes are useful for defining a common interface for a group of related classes.

Get DAVID SAMUEL’s stories in your inbox

Join Medium for free to get updates from this writer.

For example, suppose we have a Vehicle abstract class that defines a common interface for all types of vehicles. We can define the Vehicle abstract class in Python using the abc module as follows:

from abc import ABC, abstractmethod

class Vehicle(ABC):
@abstractmethod
def start_engine(self):
pass

@abstractmethod
def stop_engine(self):
pass

@abstractmethod
def accelerate(self):
pass

@abstractmethod
def brake(self):
pass

Now, any class that inherits from the Vehicle abstract class must provide concrete implementations of the start_engine(), stop_engine(), accelerate(), and brake() methods.

Implementing abstract classes in Python

To implement an abstract class in Python, we create a subclass that inherits from the abstract class and provides concrete implementations of its abstract methods.

For example, we want to implement the Vehicle abstract class for a Car class. We can do so as follows:

class Car(Vehicle):
def start_engine(self):
print("Starting car engine...")

def stop_engine(self):
print("Stopping car engine...")

def accelerate(self):
print("Accelerating car...")

def brake(self):
print("Applying car brakes...")

Notice that we provided concrete implementations of all the abstract methods defined in the Vehicle abstract class.

Differences between interfaces and abstract classes

Interfaces and abstract classes have some similarities, but there are also some key differences. Interfaces focus on defining a contract between a class and its users, while abstract classes focus on defining a common interface for a group of related classes.

In general, interfaces cannot define any implementation, while abstract classes can define both abstract methods and concrete methods. This means that interfaces are more restrictive than abstract classes.

Another difference between interfaces and abstract classes is that a class can implement multiple interfaces, but it can only inherit from a single abstract class. This makes interfaces more flexible than abstract classes in terms of class design.

In addition, interfaces can be used for duck typing, which means that a class is considered compatible with an interface as long as it provides the required methods, regardless of its inheritance hierarchy. Abstract classes, on the other hand, require explicit inheritance and implementation.

When to use interfaces and abstract classes

Interfaces are useful when you want to define a contract between a class and its users. They’re particularly useful for defining APIs and ensuring that classes are compatible with each other.

Abstract classes, on the other hand, are useful when you want to define a common interface for a group of related classes. They’re particularly useful for implementing a template method pattern, where the abstract class defines a skeleton for an algorithm and the concrete subclasses provide specific implementations for each step.

Conclusion

In this blog post, we explored the concepts of interfaces and abstract classes in Python. We saw that interfaces define a contract between a class and its users, while abstract classes define a common interface for a group of related classes. We also saw that interfaces are more restrictive than abstract classes and can be used for duck typing, while abstract classes require explicit inheritance and implementation. Understanding the differences between these two concepts is important for writing clean, maintainable, and flexible code in Python.

--

--

DAVID SAMUEL
DAVID SAMUEL

Written by DAVID SAMUEL

I am proficient in both front-end and back-end development, with expertise in tools like Figma, Framer, and Webflow for creating dynamic designs.