- Learning Objectives
- Prerequisites
- Introduction
- What is Abstraction?
- Why is Abstraction Required?
- Real-World Examples
- Characteristics of Abstraction
- Advantages of Abstraction
- Abstraction vs Encapsulation
- Achieving Abstraction in Python
- Abstract Class
- Abstract Method
- abc Module
- ABC Class
- @abstractmethod Decorator
- Internal Working
- Summary
After completing this chapter, you will be able to:
- Understand Abstraction.
- Explain why Abstraction is required.
- Differentiate Abstraction and Encapsulation.
- Understand Abstract Classes and Abstract Methods.
- Learn the
abcmodule. - Create Abstract Classes in Python.
- Implement Abstraction in real-world applications.
Before learning this chapter, you should know:
- Classes
- Objects
- Methods
- Inheritance
- Encapsulation
In the previous chapter, we learned Encapsulation, which combines data and methods into a single unit while controlling access to the data.
The second pillar of Object-Oriented Programming is Abstraction.
Abstraction focuses on showing only the essential features of an object while hiding unnecessary implementation details.
This makes programs easier to use, understand, and maintain.
Abstraction is the process of hiding implementation details and exposing only the essential functionality to the user.
The user knows what an object does, but does not need to know how it does it.
Showing only the required information and hiding the implementation details is called Abstraction.
Suppose you are driving a car.
You use:
- Steering Wheel
- Accelerator
- Brake
- Clutch
Do you need to know:
- How the engine works?
- How fuel is injected?
- How the gearbox operates?
No.
You only use the controls.
The complex internal implementation remains hidden.
This is Abstraction.
The user performs:
- Withdraw Money
- Deposit Money
- Check Balance
The user does not know how the bank server processes these operations internally.
The user:
- Makes Calls
- Sends Messages
- Opens Applications
The internal processing remains hidden.
The user presses:
- Power
- Volume
- Channel
The internal electronic circuits remain hidden.
- Hides implementation details.
- Shows only essential functionality.
- Reduces complexity.
- Improves code readability.
- Makes software easier to maintain.
- Reduces code complexity.
- Improves security.
- Easier maintenance.
- Better code organization.
- Supports modular programming.
- Makes software reusable.
| Abstraction | Encapsulation |
|---|---|
| Hides implementation | Hides data |
| Focuses on what an object does | Focuses on protecting data |
| Achieved using Abstract Classes and Interfaces | Achieved using Classes and Access Control |
| Simplifies usage | Improves security |
Python provides abstraction using the abc module.
Important components are:
- Abstract Class
- Abstract Method
- ABC
- @abstractmethod
An Abstract Class is a class that cannot be instantiated directly.
It is designed to act as a blueprint for other classes.
It may contain:
- Abstract Methods
- Concrete (Normal) Methods
An Abstract Method is a method that is declared but does not contain an implementation.
Every child class must implement the abstract method.
Python provides a built-in module named
abcwhich stands for
Abstract Base Classes
It provides support for creating Abstract Classes.
To create an Abstract Class,
inherit from
ABCExample
from abc import ABC
class Vehicle(ABC):
passTo declare an Abstract Method,
use
@abstractmethodExample
from abc import ABC, abstractmethod
class Vehicle(ABC):
@abstractmethod
def start(self):
passEvery child class must provide an implementation for start().
Create Abstract Class
↓
Declare Abstract Method
↓
Create Child Class
↓
Override Abstract Method
↓
Create Child Object
↓
Call Implemented Method
In this part, we learned:
- Abstraction
- Need for Abstraction
- Real-World Examples
- Abstract Class
- Abstract Method
- abc Module
- ABC
- @abstractmethod
- Internal Working
Write a Python program to create an Abstract Class.
from abc import ABC, abstractmethod
class Vehicle(ABC):
@abstractmethod
def start(self):
passVehicleis an Abstract Class.start()is an Abstract Method.- Since
Vehiclecontains an Abstract Method, objects of this class cannot be created.
Write a Python program to implement an Abstract Method in a child class.
from abc import ABC, abstractmethod
class Vehicle(ABC):
@abstractmethod
def start(self):
pass
class Car(Vehicle):
def start(self):
print("Car Started")
car = Car()
car.start()Car Started
The child class Car provides an implementation for the abstract method start().
Since all abstract methods are implemented, an object of Car can be created.
Write a Python program to demonstrate that an Abstract Class cannot be instantiated.
from abc import ABC, abstractmethod
class Vehicle(ABC):
@abstractmethod
def start(self):
pass
vehicle = Vehicle()TypeError:
Can't instantiate abstract class Vehicle
with abstract method start
Python prevents creating objects of Abstract Classes.
Only child classes that implement all abstract methods can be instantiated.
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
@abstractmethod
def perimeter(self):
pass
class Rectangle(Shape):
def area(self):
print("Area Calculated")
def perimeter(self):
print("Perimeter Calculated")
rectangle = Rectangle()
rectangle.area()
rectangle.perimeter()Area Calculated
Perimeter Calculated
An Abstract Class may contain multiple Abstract Methods.
The child class must implement all abstract methods.
from abc import ABC, abstractmethod
class Animal(ABC):
@abstractmethod
def sound(self):
pass
def sleep(self):
print("Animal is Sleeping")
class Dog(Animal):
def sound(self):
print("Dog Barks")
dog = Dog()
dog.sound()
dog.sleep()Dog Barks
Animal is Sleeping
An Abstract Class can contain:
- Abstract Methods
- Normal (Concrete) Methods
The child class inherits the normal methods.
Create Abstract Class
↓
Declare Abstract Method
↓
Create Child Class
↓
Override Abstract Method
↓
Create Child Object
↓
Call Method
Animal (ABC)
+----------------------+
| sound() |
| sleep() |
+----------------------+
▲
│
Dog Object
│
▼
+----------------------+
| sound() |
+----------------------+
- Hides implementation details.
- Reduces code complexity.
- Improves maintainability.
- Encourages code reusability.
- Supports modular programming.
- Defines a common structure for child classes.
- Use Abstract Classes when multiple child classes share common behavior.
- Declare only essential methods as abstract.
- Avoid unnecessary abstraction in small programs.
- Use meaningful method names.
- Keep common functionality in concrete methods.
Creating an object of an Abstract Class.
❌ Incorrect
vehicle = Vehicle()Produces
TypeError
Not implementing Abstract Methods.
class Car(Vehicle):
passThe child class remains abstract and cannot be instantiated.
Forgetting to inherit from ABC.
class Vehicle:Without inheriting from ABC, Python will not treat the class as an Abstract Class.
Forgetting the @abstractmethod decorator.
Without the decorator, the method becomes a normal method instead of an abstract one.
Abstraction is used in:
- Payment Gateway Systems
- Banking Applications
- Vehicle Management Systems
- Hospital Management Systems
- GUI Frameworks
- Game Development
- Enterprise Software
Abstraction is the process of hiding implementation details while exposing only the essential functionality.
An Abstract Class is a class that cannot be instantiated directly and serves as a blueprint for child classes.
An Abstract Method is a method declared without implementation. Every child class must implement it.
The built-in abc module.
Abstract Base Class.
It marks a method as abstract, requiring all child classes to provide an implementation.
Yes.
An Abstract Class can contain both abstract methods and normal (concrete) methods.
- Create an Abstract Class named
Employee. - Create an Abstract Class with two Abstract Methods.
- Create a
ShapeAbstract Class and implement it usingCircle. - Demonstrate that an Abstract Class cannot be instantiated.
- Create an
AnimalAbstract Class with one abstract method and one normal method.
- Abstraction hides implementation details.
- Use the
abcmodule for abstraction. - Inherit from
ABCto create an Abstract Class. - Use
@abstractmethodto declare Abstract Methods. - Abstract Classes cannot be instantiated.
- Child classes must implement all Abstract Methods.
In this chapter, you learned:
- Abstraction
- Need for Abstraction
- Abstract Classes
- Abstract Methods
abcModuleABC@abstractmethod- Internal Working
- Practical Programs
- Best Practices
- Common Mistakes
- Interview Questions
- Practice Programs
- Quick Revision