Abstraction

In simple terms, abstraction “displays” only the relevant attributes of objects and “hides” the unnecessary details.

Untitled

Abstraction is when we separate what from how.  This is a very commonly used principle when designing devices. For example, think of a TV. We know that we can watch visual images with sound, change between different channels, change volume, etc. However, most of us do not understand HOW these things are happening inside the device. This is because we have been provided with a friendly interface to interact with the TV - the set of buttons in front of the tv or the remote control. Therefore, the HOW part of the TV is entirely hidden by the user. The same principle applies to other devices such as telephones and air conditioners.

This same concept is widely used in OOP.  With respect to an object, the other classes/objects know what the object does and how to use it, but not how the object actually operates. For example, suppose there is an Employee superclass and a set of subclasses such as Teacher, Postman, and Driver. The rest of the code should know that each Employee object has a work() method. However, the rest of the code does not know and does not need to know exactly how each employee subclass object implements the work() method. This implementation detail is accessible only within the Employee subclasses. Abstraction is very helpful in managing code complexity and code changes. This is because the internals of a class can be changed without impacting the rest of the code. And we will learn next how abstraction helps code reuse as well.

Now you might be thinking that abstraction and encapsulation are just the same. It is not.  Compared to encapsulation, we focus on abstraction at the design level. Abstraction lets us focus on what the object does, not how it does that. Encapsulation means hiding internal details of how an object does something. Moreover, in OOP, abstraction is more meaningful when there is an inheritance relationship among the considered classes; the superclass indicates WHAT the objects of its subclasses can do. The subclasses specify the actual implementation of the methods. In contrast, encapsulation exists in the context of a single class. In abstraction, we mainly focus on object behavior, in other words, methods. In contrast, in encapsulation, we mainly focus on attributes.

In Python, abstraction is achieved by abstract classes. An abstract class is like an ordinary class; the only thing is that it cannot be instantiated. In other words, we cannot create objects from an abstract class. Then you might wonder, then how can we make use of an abstract class. Let’s find out.

The concept of abstract classes is meaningful only in a subclass superclass relationship. In other words, we can have a superclass that contains the common functionality of a collection of classes. When we actually create objects, we have to create objects from the subclasses, not the superclass. For example, think of two types of vehicles - cars and bikes. Although we talk about vehicles, what we actually have are cars and bikes. And there is no need, and sometimes not even possible, to create vehicle objects. Why is this? Because some features of an object are properly defined in a subclass only. For example, although we know that both cars and bikes can move, how the car moves is different from how the bike moves. At the vehicle class level, we know that the vehicle can move. However, how the actual movement happens is determined by the subclass. We cannot create objects from the superclass in such scenarios, so they should be kept abstract. The abstract class can specify WHAT all subclass objects can do - move. HOW this move method is implemented is defined by the individual subclass car or bike. Therefore, we can create an abstract class called Vehicle and include the move method as an abstract method.

To declare an abstract class in Python, you need to extend the ABC class, which is part of the Python ABC module. Then we define an abstract method using the @abstractmethod decorator. In Python, we can simply include the pass keyword as the method body to show that the method does not do anything meaningful. Abstract classes can have abstract methods as well as concrete methods.

Untitled

Untitled

Untitled

Polymorphism

polymorphism is describes situations in which something occurs in several different forms.

Polymorphism means an object being able to behave in different forms. In Python, we can find polymorphism in many different situations. Three such situations are polymorphism with inbuilt functions or operators, polymorphism with user-defined functions, and polymorphism with inheritance.