Wednesday, March 23, 2022

Abstraction

Abstraction is another powerful feature of OOP and is mainly related to hide the details of the implementation and show only the essential or high-level features of an object. A real-world example is a car that we derive with the main features available to us as a driver, without knowing the real details of how the feature works and which other objects are involved to provide these features.

Abstraction is a concept that is related to encapsulation and inheritance together, and that is why we have kept this topic till the end to understand encapsulation and inheritance first. Another reason for having this as a separate topic is to emphasize the use of abstract classes in Python.

Abstract classes in Python

An abstract class acts like a blueprint for other classes. An abstract class allows you to create a set of abstract methods (empty) that are to be implemented by a child class. In simple terms, a class that contains one or more abstract methods is called an abstract class. On the other hand, an abstract method is one that only has a declaration but no implementation.

There can be methods in an abstract class that are already implemented and that can be leveraged by a child class (as is) using inheritance. The concept of abstract classes is useful to implement common interfaces such as application programming interfaces (APIs) and also to define a common code base in one place that can be reused by child classes.

An abstract class can be implemented using a Python built-in module called Abstract Base Classes (ABC) from the abc package. The abc package also includes the Abstractmethod module, which utilizes decorators to declare the abstract methods.

A simple Python example with the use of the ABC module and the abstractmethod decorator is shown next:

#abstraction1.py
from abc import ABC, abstractmethod
class Vehicle(ABC):
def hello(self):
print(f"Hello from abstract class")
@abstractmethod
def print_me(self):
pass
class Car (Vehicle):
def __init__(self, color, seats):
self.i_color = color
self.i_seats = seats
"""It is must to implemented this method"""
def print_me(self):
print( f"Car with color {self.i_color} and no of \
seats {self.i_seats}")
if __name__ == "__main__":
# vehicle = Vehicle() #not possible
# vehicle.hello()
car = Car ("blue", 5)
car.print_me()
car.hello() 

In this example, we did the following:

• We made the Vehicle class abstract by inheriting it from the ABC class and also by declaring one of the methods (print_me) as an abstract method. We used the @abstractmethod decorator to declare an abstract method.

• Next, we updated our famous Car class by implementing the print_me method in it and keeping the rest of the code the same as in the previous example.

• In the main part of the program, we attempted to create an instance of the Vehicle class (code commented in the illustration). We created an instance of the Car class and executed the print_me and hello methods.

When we attempt to create an instance of the Vehicle class, it gives us an error like this:

Can't instantiate abstract class Vehicle with abstract methods
print_me

Also, if we try to not implement the print_me method in the Car child class, we get an error. For an instance of the Car class, we get the expected console output from the print_me and hello methods.

Our next topic of discussion will be composition.

Share:

0 comments:

Post a Comment