Abstraction is one of the core principles of Object-Oriented Programming (OOP).
Abstraction means hiding internal implementation details and showing only the essential features to the user.
In simple words:
Abstraction focuses on what an object does, not how it does it.
Real-Life Example
When you drive a car:
- You use the steering wheel, accelerator, and brakes
- You do not need to know how the engine works internally
That is abstraction.
Why Abstraction is Important
- Reduces complexity
- Improves security
- Hides unnecessary details
- Makes code easier to maintain
Abstraction in Python
Python provides abstraction using:
- Abstract Classes
- Abstract Methods
To create an abstract class, we use the abc module.
Example of Abstraction
from abc import ABC, abstractmethodclass Shape(ABC): @abstractmethod
def area(self):
passclass Circle(Shape): def __init__(self, radius):
self.radius = radius def area(self):
return 3.14 * self.radius * self.radiusc = Circle(5)
print(c.area())
Explanation
Shapeis an abstract classarea()is an abstract methodCirclemust implement thearea()method- You cannot create an object of
Shapedirectly
If you try:
s = Shape()
It will give an error.
Key Rules of Abstraction
- Abstract class cannot be instantiated
- Abstract methods must be implemented in child class
- Use
ABCand@abstractmethod
Difference Between Encapsulation and Abstraction
Encapsulation
Hides data
Focuses on restricting access
Abstraction
Hides implementation
Focuses on showing only essential behavior
Key Takeaway
Abstraction simplifies complex systems by hiding implementation details and exposing only what is necessary.
It helps create clean, secure, and scalable OOP programs.