Abstraction is one of the core principles of Object-Oriented Programming (OOP). It means hiding the internal implementation details and showing only the essential features to the user.
In simple terms:
Abstraction focuses on what an object does, not how it does it.
Consider a car:
👉 This is abstraction — complex details are hidden from the user
Abstraction is the process of:
Python supports abstraction using:
These are implemented using the abc (Abstract Base Class) module.
An abstract class is a class that:
from abc import ABC, abstractmethod
class ClassName(ABC):
@abstractmethod
def method_name(self):
pass
An abstract method is a method that:
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def sound(self):
pass
class Dog(Animal):
def sound(self):
print("Bark")
class Cat(Animal):
def sound(self):
print("Meow")
d = Dog()
d.sound()
c = Cat()
c.sound()
Output
Bark
Meow
Animal is an abstract classsound() is an abstract methodDog, Cat) must implement the methodAnimalABC@abstractmethod decorator| Feature | Abstraction | Encapsulation |
|---|---|---|
| Purpose | Hides implementation | Hides data |
| Focus | What to show | How to protect |
| Implementation | Abstract classes | Private variables |
abc module in Python.