Abstraction in Python: A Complete Guide | FACE Prep

Abstraction in Python: A Complete Guide | FACE Prep

Abstraction in Python | FACE Prep

What is Abstraction?

Abstraction in Python involves hiding the real implementation details of a system from the user while emphasizing its usage. It simplifies complex processes and makes them accessible without delving into how they work.

Everyday Examples of Abstraction:

  1. Electronic Gadgets: When you buy a new gadget, the user guide explains how to use it without detailing the internal mechanisms.
  2. TV Remote: Pressing a button on the remote changes the channel or adjusts the volume without understanding the internal electronic workings.

Why Do We Need Abstraction?

Abstraction helps programmers:
  • Reduce complexity by hiding irrelevant details.
  • Focus on the essential features of an application.
  • Increase efficiency and readability in programs.

How to Achieve Abstraction in Python

Abstraction in Python is primarily achieved through abstract classes and methods.

Abstract Classes in Python

An abstract class is a class containing one or more abstract methods. Abstract methods act as templates that must be implemented in derived classes.

Key Characteristics:

  • Abstract methods do not contain any implementation.
  • Abstract classes cannot be instantiated directly.
  • Abstract classes may include both normal and abstract methods.
To create an abstract class, Python uses the ABC module (Abstract Base Classes).

Syntax for Abstract Class

from abc import ABC

class ClassName(ABC):
    # Abstract class definition
    pass

Example: Abstract Class

from abc import ABC

class Shape(ABC):  # Abstract class
    def calculate_area(self):  # Abstract method
        pass
In this example, Shape is an abstract class, and calculate_area is an abstract method. The implementation of calculate_area will be defined in the derived classes.

Implementation of Abstract Classes

Let’s create a program to calculate the areas of a rectangle and a circle using abstract classes.

Example

from abc import ABC

class Shape(ABC):
    def calculate_area(self):
        pass

class Rectangle(Shape):
    length = 5
    breadth = 3

    def calculate_area(self):
        return self.length * self.breadth

class Circle(Shape):
    radius = 4

    def calculate_area(self):
        return 3.14 * self.radius * self.radius

# Create objects for derived classes
rec = Rectangle()
cir = Circle()

print("Area of a rectangle:", rec.calculate_area())
print("Area of a circle:", cir.calculate_area())

Output:

Area of a rectangle: 15
Area of a circle: 50.24
If the derived classes do not implement the abstract method, Python will throw an error.

Abstract Class with Both Normal and Abstract Methods

An abstract class can also include normal methods. These methods can be accessed by the objects of derived classes.

Example

from abc import ABC

class Shape(ABC):
    def print_message(self):
        print("This is a method in the abstract class Shape.")

    def calculate_area(self):
        pass

class Rectangle(Shape):
    length = 5
    breadth = 3

    def calculate_area(self):
        return self.length * self.breadth

# Create an object of the derived class
rec = Rectangle()
rec.print_message()
print("Area of a rectangle:", rec.calculate_area())

Output:

This is a method in the abstract class Shape.
Area of a rectangle: 15

Key Points to Remember

  1. Abstract classes cannot be instantiated.
  2. Derived classes must implement all abstract methods of the abstract class.
  3. Abstract classes can contain both abstract and normal methods.

Conclusion

Abstraction is a powerful tool in Python that simplifies program complexity by hiding implementation details and focusing on functionality. Using abstract classes and methods, developers can create reusable and efficient code templates, promoting modularity and maintainability.
c