Skip to main content

Command Palette

Search for a command to run...

Abstraction in Python

Hiding Complexity, Showing Only What Matters

Published
โ€ข4 min read
Abstraction in Python

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

FeatureAbstract ClassNormal Class
Object CreationโŒ Not allowedโœ… Allowed
Abstract Methodsโœ… YesโŒ No
PurposeBlueprint with rulesFull implementation
Forces Child Implementationโœ… YesโŒ No

๐Ÿง  Abstract Method vs Normal Method

Abstract MethodNormal Method
No bodyHas body
Only declarationFull logic
Must be overriddenOptional override
Used in abstract classUsed 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

  1. Create an abstract class Vehicle with abstract method start()

  2. Create child classes Car and Bike implementing start()

  3. 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 ๐Ÿš€