Difference Between Interface and Abstract Class in Python

1. Introduction

In Python, interfaces and abstract classes are two concepts used in object-oriented programming. An interface is a blueprint for designing classes. It represents a contract where the classes inheriting it must implement the methods defined in the interface. Python does not have built-in support for interfaces as in other languages but uses a concept known as a protocol. An abstract class, on the other hand, is a class that cannot be instantiated and may contain both abstract methods (which must be implemented by subclasses) and concrete methods (which have implementation).

2. Key Points

1. Purpose: Interfaces define a contract of what the classes should do, abstract classes can provide some common functionality.

2. Methods: Interface methods are typically unimplemented, abstract class methods can be partially or fully implemented.

3. Instantiation: Neither interfaces nor abstract classes can be instantiated on their own.

4. Python Implementation: Interfaces are informally defined through protocols, abstract classes use the abc module.

3. Differences

Characteristic Interface Abstract Class
Purpose Defines a contract for classes Provides common functionality
Methods Typically unimplemented Can be partially or fully implemented
Instantiation Cannot be instantiated Cannot be instantiated
Python Implementation Informal, using protocols Using abc module

4. Example

# Python does not have a built-in concept of interfaces as in other languages
# But we can create a protocol or interface-like class

# Interface Example
class MyInterface:
    def method1(self):
        pass

    def method2(self):
        pass

# Abstract Class Example
from abc import ABC, abstractmethod

class MyAbstractClass(ABC):
    @abstractmethod
    def abstract_method(self):
        pass

    def concrete_method(self):
        print("This is a concrete method.")

Output:

No direct output, as these concepts are structural in nature and not meant to be executed independently.

Explanation:

1. MyInterface acts as a blueprint for other classes, expecting them to implement method1 and method2.

2. MyAbstractClass provides a partial structure with the abstract method abstract_method which must be implemented by subclasses, and a concrete method concrete_method which is optional to override.

5. When to use?

- Use an interface (or protocol in Python) when you need multiple classes to follow a specific contract of methods, ensuring consistency across implementations.

- Use an abstract class when you need to provide common shared behavior among different subclasses, but do not want to allow instantiation of the class itself.

Related Python Posts:

Difference Between Local and Global Variables in Python

Difference Between List and Tuple in Python

Difference Between Array and List in Python

Difference Between List and Dictionary in Python

Difference Between List, Tuple, Set and Dictionary in Python

Difference Between a Set and Dictionary in Python

Difference between for loop and while loop in Python

Difference Between pass and continue in Python

Difference Between List append and extend in Python

Difference Between == and is operator in Python

Difference Between Deep and Shallow Copy in Python

Class Method vs Static Method in Python

Class Method vs Instance Method in Python

Difference Between List and Set in Python

Difference Between Generator and Iterator in Python

Difference Between str and repr in Python

Method Overloading vs Overriding in Python

Difference Between Dictionary and Tuple in Python

Difference Between Dictionary and Object in Python

Difference Between Mutable and Immutable in Python

Difference Between Interface and Abstract Class in Python

Difference Between Python Script and Module

Difference Between for Loop and Iterator in Python

Comments