Abstraction in Python
Abstraction in Python
Example -
Output:
Explanation -
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.
Example -
Output:
Explanation -
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.
o An Abstract class can contain the both method normal and abstract
method.
o An Abstract cannot be instantiated; we cannot create objects for the
abstract class.