ABSTRACTION

ABSTRACTION:

Abstraction in Python is the process of hiding the real implementation of an application from the user and emphasizing only on usage of it. For example, consider you have bought a new electronic gadget. Along with the gadget, you get a user guide, instructing how to use the application, but this user guide has no info regarding the internal working of the gadget.

Another example is, when you use TV remote, you do not know how pressing a key in the remote changes the channel internally on the TV. You just know that pressing + volume key will increase the volume.

Abstract Classes In Python

 

A class containing one or more abstract methods is called an abstract class.

Abstract methods do not contain any implementation. Instead, all the implementations can be defined in the methods of sub-classes that inherit the abstract class. An abstract class is created by importing a class named 'ABC' from the 'abc' module and inheriting the 'ABC' class. Below is the syntax for creating the abstract class.

Syntax

from abc import ABC
Class ClassName(ABC):

Example

How Abstract Base classes work :

By default, Python does not provide abstract classes. Python comes with a module which provides the base for defining Abstract Base classes(ABC) and that module name is ABC. ABC works by decorating methods of the base class as abstract and then registering concrete classes as implementations of the abstract base. A method becomes abstract when decorated with the keyword @abstractmethod. For Example –

from abc import ABC, abstractmethod

class Animal(ABC):

  

    def move(self):

        pass

  

class Human(Animal):

  

    def move(self):

        print("I can walk and run")

  

class Snake(Animal):

  

    def move(self):

        print("I can crawl")

  

class Dog(Animal):

  

    def move(self):

        print("I can bark")

  

class Lion(Animal):

  

    def move(self):

        print("I can roar")

          

# Driver code

R = Human()

R.move()

  

K = Snake()

K.move()

  

R = Dog()

R.move()

  

K = Lion()

K.move()

Output:

I can walk and run

I can crawl

I can bark

I can roar

Example-1:

from abc import ABC

Class Shape(ABC): #abstract class

Here, the class 'ABC' is imported to and inherited by the class 'shape'. The class 'Shape' becomes an abstract class when we define abstract methods in it. As we already discussed, abstract methods should not contain any implementation. So, we can define abstract methods using the 'pass' statement' as shown below.

from abc import ABC
class Shape(ABC):def calculate_area(self): #abstract methodpass

Here, calculate_area is the abstract method of the abstract class 'Shape'. The implementation of this abstract class can be defined in the sub-classes that inherit the class 'Shape'. In the below example, 'Rectangle' and 'Circle' are the two sub-classes that inherit the abstract class 'Shape'.

from abc import ABC
class Shape(ABC): #abstract classdef calculate_area(self): #abstract methodpass
 
class Rectangle(Shape):
  length = 5
  breadth = 3def calculate_area(self):return self.length * self.breadth
 
class Circle(Shape):
  radius = 4def calculate_area(self):return 3.14 * self.radius * self.radius
 
rec = Rectangle() #object created for the class 'Rectangle'
cir = Circle() #object created for the class 'Circle'
print("Area of a rectangle:", rec.calculate_area()) #call to 'calculate_area' method defined inside the class 'Rectangle'
print("Area of a circle:", cir.calculate_area()) #call to 'calculate_area' method defined inside the class 'Circle'.

Output:

Area of a rectangle: 15
Area of a circle: 50.24

If the implementation of the abstract method is not defined in the derived classes, then the Python interpreter throws an error.

 

 

 


Comments