Abstraction in Python | Hiding the Complex, Showing Only the Needed



🔹 What is Abstraction?

Abstraction means:

Hiding the complex details and showing only the important parts.

It helps us to:

  • Focus on what an object does
  • Not worry about how it does it

🔸 Real-Life Example:

Think about a TV remote:

  • You press the power button to turn on the TV.
  • You don’t need to know how electricity flows inside.

🟢 This is abstraction — showing only the button (interface), hiding the working inside.


🔹 Abstraction in Python

We use:

  • Abstract Base Class (ABC)
  • The abc module

🔹 How to Use Abstraction in Python

  1. Import ABC and abstractmethod
  2. Create a class that inherits from ABC
  3. Use @abstractmethod to define abstract methods
  4. Subclass must override these methods

🔹 Example:

from abc import ABC, abstractmethod

class Animal(ABC):  # Abstract class
    @abstractmethod
    def sound(self):  # Abstract method
        pass

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

class Cat(Animal):
    def sound(self):
        print("Cat meows")

d = Dog()
c = Cat()
d.sound()
c.sound()

🟢 Output:

Dog barks
Cat meows


🔹 Key Rules of Abstraction:

  • You cannot create an object of abstract class.
  • Child class must implement (override) abstract methods.
  • Abstract class can also have normal methods.

🔹 Abstract Class with Normal Method

from abc import ABC, abstractmethod

class Vehicle(ABC):
    @abstractmethod
    def start(self):
        pass

    def fuel_type(self):
        print("Fuel type is petrol")

class Car(Vehicle):
    def start(self):
        print("Car is starting...")

c = Car()
c.start()
c.fuel_type()


📝 Practice Questions with Answers

❓ Q1: Create an abstract class Shape with abstract method area(). Implement it in Rectangle and Circle.

✅ Answer:

from abc import ABC, abstractmethod

class Shape(ABC):
    @abstractmethod
    def area(self):
        pass

class Rectangle(Shape):
    def area(self):
        print("Area = length × width")

class Circle(Shape):
    def area(self):
        print("Area = π × radius²")

r = Rectangle()
c = Circle()
r.area()
c.area()


❓ Q2: Make an abstract class Device with turn_on() and turn_off() methods.

✅ Answer:

from abc import ABC, abstractmethod

class Device(ABC):
    @abstractmethod
    def turn_on(self):
        pass

    @abstractmethod
    def turn_off(self):
        pass

class Laptop(Device):
    def turn_on(self):
        print("Laptop is turning on")

    def turn_off(self):
        print("Laptop is shutting down")

l = Laptop()
l.turn_on()
l.turn_off()


🧾 Summary Table

TermMeaning
AbstractionHiding how things work, showing only what’s needed
Abstract ClassA class with abstract methods
Abstract MethodMethod with no body (empty function)
@abstractmethodUsed to create abstract methods

🎯 Quick Recap

  • Abstraction = Hiding details, showing only the essentials.
  • Use abc module, ABC, and @abstractmethod.
  • Helps write clean, structured, and safe code.

📣 Coming Up Next:

➡️ Difference Between Abstraction and Encapsulation in Python (Simple Words)


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *