python
  1. python-abstraction

Python Abstraction

Abstraction is one of the fundamental concepts of object-oriented programming. In Python, abstraction is a way to hide internal details and complexity of a program and only showing the relevant and necessary information to the user.

Syntax

Abstraction is achieved in Python through Abstract Classes and Interfaces.

Abstract Classes

Abstract classes in Python can be defined by using the built-in abc module. A class can be defined as abstract by using the @abc.abstractmethod decorator.

Example:

import abc

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

class Car(Vehicle):
    def start(self):
        print("Starting the car...")

c = Car()
c.start() # Output: Starting the car...

In the above example, we have defined an abstract class Vehicle using the @abc.abstractmethod decorator. The start() method of the Vehicle class is abstract, which means it has no implementation. The Car class is derived from the Vehicle class and implements the start() method.

Interfaces

Interfaces are similar to abstract classes and are defined using the built-in abc module as well.

Example:

import abc

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

class Square(Shape):
    side = 5
    def area(self):
        return self.side**2

s = Square()
print("Area of square:", s.area()) # Output: Area of square: 25

In the above example, we have defined an interface Shape using the @abc.abstractmethod decorator. The area() method of the Shape class is abstract and has no implementation. The Square class is derived from the Shape class and implements the area() method.

Explanation

Abstraction is a way to hide the underlying complexity of a program from the user and exposing only the relevant and necessary details. Abstract classes and interfaces are the two main ways to implement abstraction in Python.

An abstract class has one or more abstract methods that have no implementation. Abstract classes cannot be instantiated and must be subclassed. The abstract methods must be implemented by the subclass.

An interface is similar to an abstract class, but it only contains abstract methods. In Python, interfaces can be defined using the Abstract Base Classes (ABC) module. An interface can be implemented by any class that implements all its abstract methods.

Use

Abstraction helps to simplify the complexity of a program and make it more user-friendly. It also helps to improve the maintainability and extensibility of a program.

Abstraction is commonly used in the development of large-scale software systems where hiding internal details and complexity is essential.

Important Points

  • Abstraction is a fundamental concept of object-oriented programming.
  • Abstraction is achieved in Python through abstract classes and interfaces.
  • Abstract classes and interfaces cannot be instantiated.
  • Abstract classes define one or more abstract methods that have no implementation.
  • Abstract classes must be subclassed, and the abstract methods must be implemented by the subclass.
  • Interfaces are similar to abstract classes, but they only contain abstract methods.
  • Interfaces can be implemented by any class that implements all its abstract methods.

Summary

Abstraction is a way to hide internal details and complexity of a program and only showing the relevant and necessary information to the user. In Python, abstraction is achieved through the use of abstract classes and interfaces. Abstract classes contain one or more abstract methods that have no implementation and must be subclassed. Interfaces only contain abstract methods and can be implemented by any class that implements all its abstract methods. Abstraction simplifies the complexity of a program, improves its maintainability and extensibility, and is commonly used in the development of large-scale software systems.

Published on: