Abstraction in Python
Abstraction is the fourth major pillar of OOP.
1. What is Abstraction?
Abstraction means hiding unnecessary implementation details and showing only the essential features to the user.
Simple definition
Abstraction hides how something works and shows only what is necessary to use it.
2. Real-Life Example
Consider an ATM.
You see options such as:
Withdraw Money Deposit Money Check Balance
You don't need to know:
- How the bank server works
- How the transaction is processed
- How the database is updated
- How the ATM communicates with the bank
You only interact with the required features.
This is abstraction.
3. Abstraction in Python
Python provides abstraction mainly through the abc module.
abc means:
Abstract Base Classes
Import it using:
from abc import ABC, abstractmethod
4. Abstract Class
An abstract class is a class that is designed to be inherited by other classes.
It can contain abstract methods that child classes are required to implement.
Basic syntax
from abc import ABC, abstractmethod class Animal(ABC): @abstractmethod def sound(self): pass
Here:
Animal→ Abstract classsound()→ Abstract method
5. Abstract Method
An abstract method is a method that is declared in the parent class but does not provide the complete implementation.
It uses:
@abstractmethod
Example:
from abc import ABC, abstractmethod class Animal(ABC): @abstractmethod def sound(self): pass
The parent class says:
Every child class must provide a
sound()method.
6. Implementing an Abstract Class
A child class provides the implementation.
from abc import ABC, abstractmethod class Animal(ABC): @abstractmethod def sound(self): pass class Dog(Animal): def sound(self): print("Bark")
Now:
dog = Dog() dog.sound()
Output:
Bark
7. Why Can't We Create an Abstract Class Object?
Because Animal contains an unimplemented abstract method:
animal = Animal()
This will raise an error.
The purpose of the abstract class is to provide a common structure/interface for child classes.
8. Multiple Abstract Methods
An abstract class can have multiple abstract methods.
from abc import ABC, abstractmethod class Vehicle(ABC): @abstractmethod def start(self): pass @abstractmethod def stop(self): pass
A child class needs to implement the required abstract methods before it can be instantiated.
class Car(Vehicle): def start(self): print("Car started") def stop(self): print("Car stopped")
9. Abstract Class Can Have Normal Methods
An abstract class does not have to contain only abstract methods.
It can contain:
- Abstract methods
- Normal methods
- Attributes
- Constructors
Example:
from abc import ABC, abstractmethod class Animal(ABC): def eat(self): print("Animal is eating") @abstractmethod def sound(self): pass
Here:
eat() → Normal method sound() → Abstract method
10. Abstract Class with Constructor
An abstract class can also have __init__().
from abc import ABC, abstractmethod class Animal(ABC): def __init__(self, name): self.name = name @abstractmethod def sound(self): pass
A child class can use the parent's constructor:
class Dog(Animal): def sound(self): print(self.name, "barks")
11. Main Purpose of Abstraction
Abstraction is mainly used to:
Hide Implementation Details
Users don't need to know internal code.
Define a Common Interface
Different child classes can follow the same structure.
Force Required Methods
Abstract methods require subclasses to provide implementations.
Improve Code Organization
Large applications can be divided into clear interfaces and implementations.
12. Abstraction vs Encapsulation
These two concepts are often confused.
| Abstraction | Encapsulation |
|---|---|
| Hides implementation details | Controls access to data |
| Focuses on what an object does | Focuses on how data is accessed/protected |
| Uses abstract classes/methods | Uses classes and access conventions |
| Simplifies complexity | Protects and organizes data |
Easy way to remember
Abstraction:
What should the user see?
Encapsulation:
How should the data be protected/controlled?
13. Abstraction vs Inheritance
| Abstraction | Inheritance |
|---|---|
| Hides unnecessary implementation | Reuses existing class features |
| Defines required behavior | Creates parent-child relationship |
| Uses abstract classes/methods | Uses class Child(Parent) |
| Focuses on interface/design | Focuses on reuse/extension |
They are often used together.
14. Abstraction and Polymorphism
Abstraction can define a common interface, while polymorphism allows different classes to implement that interface differently.
Animal abstract sound() / \ / \ Dog Cat ↓ ↓ Bark Meow
The abstract class defines:
sound()
Each child provides its own implementation.
15. Types of Abstraction
In Python, you can commonly think of abstraction at two levels:
1. Data Abstraction
Hiding unnecessary details about data and exposing only required operations.
2. Procedural/Behavior Abstraction
Hiding the implementation of an operation and exposing what the operation does.
Abstract classes and methods are Python's main formal mechanism for enforcing behavioral interfaces.
16. Important Terms
| Term | Meaning |
|---|---|
ABC | Base class for defining abstract classes |
abstractmethod | Marks a method as abstract |
| Abstract class | Class intended to define a common interface |
| Abstract method | Method that subclasses are required to implement |
| Concrete class | Normal class that provides implementations |
| Interface | Expected operations/behavior of a class |
17. Complete Structure
Abstraction | +-----------+-----------+ | | Abstract Class Abstract Method | | | @abstractmethod | Child Classes | Implement methods
Short Definition for Exam
Abstraction is an OOP concept that hides unnecessary implementation details and exposes only the essential features or interface to the user.
Four OOP Pillars
You have now covered them separately:
1. Encapsulation → Control/ protect data 2. Inheritance → Reuse existing features 3. Polymorphism → One interface, many behaviors 4. Abstraction → Hide implementation details