Notes
Categories

Abstraction in Python [ English ]

< Prev Next >

1. Introduction

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.

2. Real-Life Example

Consider a car:

👉 This is abstraction — complex details are hidden from the user

3. Definition

Abstraction is the process of:

4. Abstraction in Python

Python supports abstraction using:

  1. Abstract Classes
  2. Abstract Methods

These are implemented using the abc (Abstract Base Class) module.

5. Abstract Class

5.1 Definition

An abstract class is a class that:

5.2 Syntax

from abc import ABC, abstractmethod

class ClassName(ABC):
    
    @abstractmethod
    def method_name(self):
        pass

6. Abstract Method

6.1 Definition

An abstract method is a method that:

7. Example of Abstraction

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

Explanation

8. Key Points

9. Advantages of Abstraction

10. Difference Between Abstraction and Encapsulation

Feature Abstraction Encapsulation
Purpose Hides implementation Hides data
Focus What to show How to protect
Implementation Abstract classes Private variables

11. Summary

< Prev Next >