Ugenz Academy/Tutorial

Abstraction

5 min read980 words

Reading progress

Keep learning

Abstraction in Python

Abstraction is the fourth major pillar of OOP.

1. What is Abstraction?

Abstraction means hiding unnecessary implementation details and showing only the essential features to the user.

Simple definition

Abstraction hides how something works and shows only what is necessary to use it.

2. Real-Life Example

Consider an ATM.

You see options such as:

Withdraw Money Deposit Money Check Balance

You don't need to know:

  • How the bank server works
  • How the transaction is processed
  • How the database is updated
  • How the ATM communicates with the bank

You only interact with the required features.

This is abstraction.

3. Abstraction in Python

Python provides abstraction mainly through the abc module.

abc means:

Abstract Base Classes

Import it using:

from abc import ABC, abstractmethod

4. Abstract Class

An abstract class is a class that is designed to be inherited by other classes.

It can contain abstract methods that child classes are required to implement.

Basic syntax

from abc import ABC, abstractmethod class Animal(ABC): @abstractmethod def sound(self): pass

Here:

  • Animal → Abstract class
  • sound() → Abstract method

5. Abstract Method

An abstract method is a method that is declared in the parent class but does not provide the complete implementation.

It uses:

@abstractmethod

Example:

from abc import ABC, abstractmethod class Animal(ABC): @abstractmethod def sound(self): pass

The parent class says:

Every child class must provide a sound() method.

6. Implementing an Abstract Class

A child class provides the implementation.

from abc import ABC, abstractmethod class Animal(ABC): @abstractmethod def sound(self): pass class Dog(Animal): def sound(self): print("Bark")

Now:

dog = Dog() dog.sound()

Output:

Bark

7. Why Can't We Create an Abstract Class Object?

Because Animal contains an unimplemented abstract method:

animal = Animal()

This will raise an error.

The purpose of the abstract class is to provide a common structure/interface for child classes.

8. Multiple Abstract Methods

An abstract class can have multiple abstract methods.

from abc import ABC, abstractmethod class Vehicle(ABC): @abstractmethod def start(self): pass @abstractmethod def stop(self): pass

A child class needs to implement the required abstract methods before it can be instantiated.

class Car(Vehicle): def start(self): print("Car started") def stop(self): print("Car stopped")

9. Abstract Class Can Have Normal Methods

An abstract class does not have to contain only abstract methods.

It can contain:

  • Abstract methods
  • Normal methods
  • Attributes
  • Constructors

Example:

from abc import ABC, abstractmethod class Animal(ABC): def eat(self): print("Animal is eating") @abstractmethod def sound(self): pass

Here:

eat() → Normal method sound() → Abstract method

10. Abstract Class with Constructor

An abstract class can also have __init__().

from abc import ABC, abstractmethod class Animal(ABC): def __init__(self, name): self.name = name @abstractmethod def sound(self): pass

A child class can use the parent's constructor:

class Dog(Animal): def sound(self): print(self.name, "barks")

11. Main Purpose of Abstraction

Abstraction is mainly used to:

Hide Implementation Details

Users don't need to know internal code.

Define a Common Interface

Different child classes can follow the same structure.

Force Required Methods

Abstract methods require subclasses to provide implementations.

Improve Code Organization

Large applications can be divided into clear interfaces and implementations.

12. Abstraction vs Encapsulation

These two concepts are often confused.

AbstractionEncapsulation
Hides implementation detailsControls access to data
Focuses on what an object doesFocuses on how data is accessed/protected
Uses abstract classes/methodsUses classes and access conventions
Simplifies complexityProtects and organizes data

Easy way to remember

Abstraction:

What should the user see?

Encapsulation:

How should the data be protected/controlled?

13. Abstraction vs Inheritance

AbstractionInheritance
Hides unnecessary implementationReuses existing class features
Defines required behaviorCreates parent-child relationship
Uses abstract classes/methodsUses class Child(Parent)
Focuses on interface/designFocuses on reuse/extension

They are often used together.

14. Abstraction and Polymorphism

Abstraction can define a common interface, while polymorphism allows different classes to implement that interface differently.

Animal abstract sound() / \ / \ Dog Cat ↓ ↓ Bark Meow

The abstract class defines:

sound()

Each child provides its own implementation.

15. Types of Abstraction

In Python, you can commonly think of abstraction at two levels:

1. Data Abstraction

Hiding unnecessary details about data and exposing only required operations.

2. Procedural/Behavior Abstraction

Hiding the implementation of an operation and exposing what the operation does.

Abstract classes and methods are Python's main formal mechanism for enforcing behavioral interfaces.

16. Important Terms

TermMeaning
ABCBase class for defining abstract classes
abstractmethodMarks a method as abstract
Abstract classClass intended to define a common interface
Abstract methodMethod that subclasses are required to implement
Concrete classNormal class that provides implementations
InterfaceExpected operations/behavior of a class

17. Complete Structure

Abstraction | +-----------+-----------+ | | Abstract Class Abstract Method | | | @abstractmethod | Child Classes | Implement methods

Short Definition for Exam

Abstraction is an OOP concept that hides unnecessary implementation details and exposes only the essential features or interface to the user.

Four OOP Pillars

You have now covered them separately:

1. Encapsulation → Control/ protect data 2. Inheritance → Reuse existing features 3. Polymorphism → One interface, many behaviors 4. Abstraction → Hide implementation details

Keep learning. Keep building. Keep growing.

© 2026 Ugenz Academy