If you are interested to learn about the second largest number in Python
Abstraction is used to hide the internal functionality of the function from the users. Abstraction in Python is the process of hiding the real implementation of an application from the user and emphasizing only on usage of it. The users only interact with the basic implementation of the function, but inner working is hidden. User is familiar with that “what function does” but they don’t know “how it does.”
In simple words, we all use the smartphone and very much familiar with its functions such as camera, voice-recorder, call-dialing, etc., but we don’t know how these operations are happening in the background. Let’s take another example – When we use the TV remote to increase the volume. We don’t know how pressing a key increases the volume of the TV. We only know to press the “+” button to increase the volume. That is exactly the abstraction that works in the object-oriented concept.
Is there abstraction in Python?
Data Abstraction in Python can be achieved through creating abstract classes and inheriting them later. Before discussing what abstract classes are, let us have a brief introduction of inheritance. Inheritance in OOP is a way through which one class inherits the attributes and methods of another class
Why Abstraction is Important?
In Python, an abstraction is used to hide the irrelevant data/class in order to reduce the complexity. It also enhances the application efficiency. Next, we will learn how we can achieve abstraction using the Python program.
Why do we need abstraction in Python?
An abstract class can be considered as a blueprint for other classes. It allows you to create a set of methods that must be created within any child classes built from the abstract class. A class which contains one or more abstract methods is called an abstract class
Abstraction classes in Python
In Python, abstraction can be achieved by using abstract classes and interfaces. A class that consists of one or more abstract method is called the abstract class. Abstract methods do not contain their implementation. Abstract class can be inherited by the subclass and abstract method gets its definition in the subclass. Abstraction classes are meant to be the blueprint of the other class. An abstract class can be useful when we are designing large functions. An abstract class is also helpful to provide the standard interface for different implementations of components. Python provides the abc module to use the abstraction in the Python program. Let’s see the following syntax.
from abc import ABC class ClassName(ABC):
We import the ABC class from the abc module.
Abstract Base Classes
An abstract base class is the common application program of the interface for a set of subclasses. It can be used by the third-party, which will provide the implementations such as with plugins. It is also beneficial when we work with the large code-base hard to remember all the classes.
Working of the Abstract Classes
Unlike the other high-level language, Python doesn’t provide the abstract class itself. We need to import the abc module, which provides the base for defining Abstract Base classes (ABC). The ABC works by decorating methods of the base class as abstract. It registers concrete classes as the implementation of the abstract base. We use the @abstractmethod decorator to define an abstract method or if we don’t provide the definition to the method, it automatically becomes the abstract method. Let’s understand the following 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()
The mileage is 30kmph The mileage is 27kmph The mileage is 25kmph The mileage is 24kmph
In the above code, we have imported the abc module to create the abstract base class. We created the Car class that inherited the ABC class and defined an abstract method named mileage(). We have then inherited the base class from the three different subclasses and implemented the abstract method differently. We created the objects to call the abstract method. Let’s understand another 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()
Triangle has 3 sides Square has 4 sides Pentagon has 5 sides Hexagon has 6 sides
In the above code, we have defined the abstract base class named Polygon and we also defined the abstract method. This base class inherited by the various subclasses. We implemented the abstract method in each subclass. We created the object of the subclasses and invoke the sides() method. The hidden implementations for the sides() method inside the each subclass comes into play. The abstract method sides() method, defined in the abstract class, is never invoked.
Points to Remember
Below are the points which we should remember 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 essential to hide the core functionality from the users. We have covered the all the basic concepts of Abstraction in Python.
How Abstract Base classes work :
By default, Python does not provide abstract classes. Python comes with a module that 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 –
I have 3 sides I have 4 sides I have 5 sides I have 6 sides
I can walk and run I can crawl I can bark I can roar
Implementation Through Subclassing :
By subclassing directly from the base, we can avoid the need to register the class explicitly. In this case, the Python class management is used to recognize PluginImplementation as implementing the abstract PluginBase.
A side-effect of using direct subclassing is, it is possible to find all the implementations of your plugin by asking the base class for the list of known classes derived from it.
Concrete Methods in Abstract Base Classes :
Concrete classes contain only concrete (normal)methods whereas abstract classes may contain both concrete methods and abstract methods. The concrete class provides an implementation of abstract methods, the abstract base class can also provide an implementation by invoking the methods via super(). Let look over the example to invoke the method using super():
Abstract Base Class subclass
In the above program, we can invoke the methods in abstract classes by using super().
Abstract Properties :
Abstract classes include attributes in addition to methods, you can require the attributes in concrete classes by defining them with @abstractproperty.
Can't instantiate abstract class parent with abstract methods geeks child class
In the above example, the Base class cannot be instantiated because it has only an abstract version of the property getter method.
Abstract Class Instantiation :
Abstract classes are incomplete because they have methods that have nobody. If python allows creating an object for abstract classes then using that object if anyone calls the abstract method, but there is no actual implementation to invoke. So we use an abstract class as a template and according to the need, we extend it and build on it before we can use it. Due to the fact, an abstract class is not a concrete class, it cannot be instantiated. When we create an object for the abstract class it raises an error.
Traceback (most recent call last): File "/home/ffe4267d930f204512b7f501bb1bc489.py", line 19, in c=Animal() TypeError: Can't instantiate abstract class Animal with abstract methods move