Learn Python Programming

Python – Abstraction


Let’s take a simple example of car to understand abstraction concept. Whenever we drive a car and we push the brake, it stop. So, the car provided has given us the feature and facility to stop the car by pushing the brake. They have not given any implementation or working of brakes that how the wires of brakes are connected internally. So they are not giving any implementation detail except the idea of feature that if you push the brake the car will stop.

Another example: Suppose you are going to an ATM to withdraw some money. What you do there? You just insert your ATM card and click some buttons and you get the money. So, here you don’t need to know their background process.

Another example: When you turn on the fan, you only know the fan is rotating. But, you do not know the actual mechanism behind it.

The abstraction is simplifying complex reality by modeling classes appropriate to the problem Class abstraction means to separate class implementation from the use of the class.

In simple words, Abstraction describes the implementation hiding.

How can we achieve abstraction in Python. We have abstract class and methods.

What is Abstract Class?

  • Abstract class are classes that contain one or more abstract methods.
  • Abstract class may contain normal method.
  • An abstract method is a method that is declared, but contains no implementation.
  • Abstract class can not be instantiated, and require sub classes to provide implementations for the abstract methods.
  • Subclasses of an abstract class in Python are not required to implement abstract methods of the parent class.

Concrete Class

A class without any abstract methods is called concrete class.

How to create an abstract class?

Abstract class => Concrete class (inherited from absolute class) + Implement or define all the abstract methods available in abstract class.

In python, there is a predefined class ABC which is an abstract base class. ABC is itself the name of an abstract class which is predefined in python. So, if you need to create an abstract class you must extend this ABC class.

Let’s look at the given example below.

from abc import ABC,abstractmethod

#This is abstract class...
class AbstractDemo(ABC):
    @abstractmethod
    def display(self):
        None

    @abstractmethod
    def show(self):
        None

#This is concrete class...
class Demo(AbstractDemo):
    def display(self):
        print("Abstract Method")
    def show(self):
        print("Show Method")

obj = Demo()
obj.display()
obj.show()


#OUTPUT
Abstract Method
Show Method

In the above example, the concrete class Demo which is inherited from the Aclass. And the display() method of abstract class get overridden in the concrete class by defining the same method in inherited class of abstract method in abstract class. And in concrete class we do write the definition of the abstract method.

If a class inheriting from an abstract class, but did not define all the abstract method in their own class, it becomes abstract class also. Until all the abstract methods are defined in an inherited class it remains abstract class. Once all the abstract methods of an abstract class are overridden in the child class, the child class called the concrete class.