Abstraction in Python
Hiding Complexity, Showing Only What Matters

When we build software, not everything needs to be visible to the user.
Think about using a mobile phone.
You tap an app icon โ the app opens.
You donโt see the thousands of lines of code running behind the scenes.
You only see what you need.
This idea โ showing only essential details and hiding internal complexity โ is called Abstraction.
It is one of the four major pillars of Object-Oriented Programming (OOP).
๐ง What is Abstraction?
Abstraction means:
Hiding internal implementation details and exposing only the necessary functionality.
In simple words:
You focus on what an object does, not how it does it.
๐ Real-Life Example โ Driving a Car
When you drive a car:
You press the accelerator โ car moves
You press the brake โ car stops
You donโt need to understand:
Engine combustion
Fuel injection system
Mechanical components
The car hides the complexity.
That is abstraction.
๐งฉ Abstract Classes โ The Blueprint with Rules
An abstract class is a special type of class that:
โ Defines structure
โ Contains abstract methods
โ Cannot be instantiated (no objects allowed)
It acts like a rulebook for other classes.
It tells child classes:
โYou must implement these methods.โ
๐ง Abstract Methods โ Methods Without Implementation
An abstract method is a method that:
Is declared
Has no body
Must be implemented in child classes
Example idea:
Every animal must make a sound โ but each animalโs sound is different.
So we define the rule, not the behavior.
๐งโ๐ป How Python Implements Abstraction
Python provides abstraction using the abc module.
We import:
from abc import ABC, abstractmethod
ABC โ Abstract Base Class
@abstractmethod โ decorator to define abstract methods
๐๏ธ Creating an Abstract Class โ Step by Step
Step 1: Import Required Module
from abc import ABC, abstractmethod
Step 2: Create Abstract Class
class Animal(ABC):
@abstractmethod
def sound(self):
pass
Here:
Animal โ abstract class
sound() โ abstract method
pass โ no implementation
It simply says:
Every animal must have a sound method.
โ ๏ธ Important Rule โ Cannot Create Object
You cannot do this:
a = Animal() # โ Error
Why?
Because the class is incomplete.
It only defines rules.
๐ถ Creating Child Classes
Now we create real implementations.
class Dog(Animal):
def sound(self):
print("Dog barks")
class Cat(Animal):
def sound(self):
print("Cat meows")
Each child class provides its own implementation.
โถ๏ธ Using the Classes
d = Dog()
c = Cat()
d.sound()
c.sound()
Output:
Dog barks
Cat meows
Now everything works perfectly.
๐จ What Happens If We Donโt Implement?
If a child class does not implement the abstract method:
class Dog(Animal):
pass
d = Dog()
Python will raise an error.
Because:
You broke the contract defined by the abstract class.
๐ฏ Why Do We Use Abstraction?
Abstraction helps us:
โ Reduce complexity
โ Enforce structure
โ Improve maintainability
โ Create scalable architecture
โ Separate interface from implementation
It is heavily used in:
Frameworks
APIs
Large applications
System design
๐ Real-Life Example โ Remote Control
A remote control has buttons:
power()
volume_up()
volume_down()
But TV, AC, and Projector implement them differently.
Remote โ Abstract class
Devices โ Child classes
The user presses buttons without knowing internal logic.
That is abstraction.
๐ Abstract Class vs Normal Class
| Feature | Abstract Class | Normal Class |
| Object Creation | โ Not allowed | โ Allowed |
| Abstract Methods | โ Yes | โ No |
| Purpose | Blueprint with rules | Full implementation |
| Forces Child Implementation | โ Yes | โ No |
๐ง Abstract Method vs Normal Method
| Abstract Method | Normal Method |
| No body | Has body |
| Only declaration | Full logic |
| Must be overridden | Optional override |
| Used in abstract class | Used anywhere |
๐ Memory Trick
Abstract = Incomplete Blueprint
Concrete Class = Complete Object
โจ When Should You Use Abstraction?
Use abstraction when:
โ Multiple classes share a common structure
โ Implementation differs across classes
โ You want strict rules for developers
โ You are designing large systems
๐งช Practice Questions
Beginner Level
Create an abstract class
Vehiclewith abstract methodstart()Create child classes
CarandBikeimplementingstart()Print different messages for both
Intermediate Level
Create an abstract class Payment with:
pay(amount)
refund(amount)
Implement:
CreditCardPayment
UPIBasedPayment
Thinking Question
Why is abstraction useful in large software projects?
๐ Final Thoughts
If:
Encapsulation protects data ๐
Inheritance shares code ๐
Polymorphism changes behavior ๐ญ
Then:
Abstraction hides complexity and enforces structure ๐ฏ
It allows developers to build powerful systems without exposing unnecessary details.
๐ Whatโs Next?
๐ Composition vs Inheritance โ Which One Should You Use?
Thatโs where OOP design becomes even more interesting ๐



