Learn Python – Abstraction in Python- Basic and advance

Abstraction is used to hide the interior performance of the characteristic from the users. The users only interact with the fundamental implementation of the function, but inner working is hidden. User is acquainted with that “what feature does” but they don’t understand “how it does.”

In simple words, we all use the smartphone and very a good deal acquainted with its features such as camera, voice-recorder, call-dialing, etc., but we do not recognize how these operations are occurring in the background. Let’s take every other example – When we use the TV faraway to extend the volume. We don’t understand how urgent a key increases the quantity of the TV. We only comprehend to press the “+” button to enlarge the volume.

That is exactly the abstraction that works in the object-oriented concept.

Why Abstraction is Important?

In Python, an abstraction is used to cover the inappropriate data/class in order to minimize the complexity. It also enhances the application efficiency. Next, we will learn how we can obtain abstraction the use of the Python program.

Abstraction classes in Python

In Python, abstraction can be done via the usage of summary lessons and interfaces.

A category that consists of one or greater abstract approach is referred to as the abstract class. Abstract techniques do now not comprise their implementation. Abstract classification can be inherited by means of the subclass and abstract method receives its definition in the subclass. Abstraction instructions are meant to be the blueprint of the different class. An summary category can be beneficial when we are designing large functions. An summary category is also beneficial to provide the standard interface for specific implementations of components. Python offers the abc module to use the abstraction in the Python program. Let’s see the following syntax.

Syntax

from abc import ABC  
class ClassName(ABC):  

We import the ABC class from the abc module.

Abstract Base Classes

An abstract base category is the common utility application of the interface for a set of subclasses. It can be used through the third-party, which will furnish the implementations such as with plugins. It is additionally beneficial when we work with the giant code-base difficult to bear in mind all the classes.

Working of the Abstract Classes

Unlike the different high-level language, Python does not furnish the summary classification itself. We need to import the abc module, which provides the base for defining Abstract Base lessons (ABC). The ABC works via adorning strategies of the base class as abstract. It registers concrete instructions as the implementation of the abstract base. We use the @abstractmethod decorator to define an abstract approach or if we do not furnish the definition to the method, it mechanically becomes the abstract method. Let’s recognize the following example.

Example –

# Python program demonstrate  
# abstract base class work   
from abc import ABC, abstractmethod   
class Car(ABC):   
    def mileage(self):   
        pass  
  
class Tesla(Car):   
    def mileage(self):   
        print("The mileage is 30kmph")   
class Suzuki(Car):   
    def mileage(self):   
        print("The mileage is 25kmph ")   
class Duster(Car):   
     def mileage(self):   
          print("The mileage is 24kmph ")   
  
class Renault(Car):   
    def mileage(self):   
            print("The mileage is 27kmph ")   
          
# Driver code   
t= Tesla ()   
t.mileage()   
  
r = Renault()   
r.mileage()   
  
s = Suzuki()   
s.mileage()   
d = Duster()   
d.mileage()  

Output:

The mileage is 30kmph
The mileage is 27kmph 
The mileage is 25kmph 
The mileage is 24kmph

Explanation –

In the above code, we have imported the abc module to create the summary base class. We created the Car category that inherited the ABC classification and described an summary approach named mileage(). We have then inherited the base type from the three different subclasses and applied the abstract technique differently. We created the objects to call the summary method.

Let’s understand another example.

Let’s understand another example.

Example –

# Python program to define   
# abstract class  
  
from abc import ABC  
  
class Polygon(ABC):   
  
   # abstract method   
   def sides(self):   
      pass  
  
class Triangle(Polygon):   
  
     
   def sides(self):   
      print("Triangle has 3 sides")   
  
class Pentagon(Polygon):   
  
     
   def sides(self):   
      print("Pentagon has 5 sides")   
  
class Hexagon(Polygon):   
  
   def sides(self):   
      print("Hexagon has 6 sides")   
  
class square(Polygon):   
  
   def sides(self):   
      print("I have 4 sides")   
  
# Driver code   
t = Triangle()   
t.sides()   
  
s = square()   
s.sides()   
  
p = Pentagon()   
p.sides()   
  
k = Hexagon()   
K.sides()   

Output:

Triangle has 3 sides
Square has 4 sides
Pentagon has 5 sides
Hexagon has 6 sides

Explanation –

In the above code, we have defined the abstract base category named Polygon and we additionally described the summary method. This base type inherited by using the a range of subclasses. We implemented the abstract approach in each subclass. We created the object of the subclasses and invoke the sides() method. The hidden implementations for the sides() technique inner the each subclass comes into play. The summary technique sides() method, defined in the summary class, is in no way invoked.

Points to Remember

Below are the points which we ought to bear in mind about the abstract base class in Python.

An Abstract class can contain the both method normal and abstract method.

An Abstract cannot be instantiated; we cannot create objects for the abstract class.

Abstraction is crucial to cover the core performance from the users. We have covered the all the primary standards of Abstraction in Python.