Basic Concepts of Object-Oriented Programming in Python

unittest

Object-Oriented Programming(OOP), is all about creating “objects”. An object is a group of interrelated variables and functions. These variables are often referred to as properties of the object and functions are referred to as the behavior of the objects. These objects provide a better and clear structure for the program.
For example, a car can be an object. If we consider the car as an object then its properties would be – its color, its model, its price, its brand, etc. And its behavior/function would be acceleration, slowing down, gear change. Another example- If we consider a dog as an object then its properties would be- his color, his breed, his name, his weight, etc. And his behavior/function would be walking, barking, playing, etc. Object-Oriented programming is famous because it implements the real-world entities like objects, hiding, inheritance, etc in programming. It makes visualization easier because it is close to real-world scenarios.
To know the basic OOPs terminology, refer Article 1

Main Concepts of Object-Oriented Programming (OOPs)

Encapsulation

Encapsulation is one of the fundamental concepts in object-oriented programming (OOP). It describes the idea of wrapping data and the methods that work on data within one unit. This puts restrictions on accessing variables and methods directly and can prevent the accidental modification of data. To prevent accidental change, an object’s variable can only be changed by an object’s method. Those types of variables are known as private variable.
Protected members (in C++ and JAVA) are those members of the class that cannot be accessed outside the class but can be accessed from within the class and its subclasses. To accomplish this in Python, just follow the convention by prefixing the name of the member by underscore “_”.
Example of Encapsulation using the Calculation Class in Calculator Program

unittest

In the Calculation class of the Calculator program, we are using the def __init__ which is a reserved method in python classes. It is called as a constructor in object-oriented terminology. This method is called when an object is created from a class, and it allows the class to initialize the attributes of the class.
When a single "_" is used, that signifies a private variable, as we cannot explicitly define private in Python. The "_values" is the nomenclature for encapsulation in the Calculation class.
We set the values as a tuple which means that they cannot be changed once applied. The values in the calculation class cannot be changed from outside and thus the above example clearly demonstrates Encapsulation

Inheritance

Inheritance is the capability of one class to derive or inherit the properties from another class. Inheritance provides re-usability of a code. We don’t have to write the same code again and again. Also, it allows us to add more features to a class without modifying it. It is also transitive in nature, which means that if class B inherits from another class A, then all the subclasses of B would automatically inherit from class A.

Example of Encapsulation using the Addition Class in Calculator Program

unittest

In the above addition class, we are creating a function that will give us the sum of two or more values. This Addition class can now be inherited in other classes, we would not need to write the code to add two or more numbers again and the class can be inherited in other classes and be reused again and again.
Reusing the Addition class in Calculator Class to build a calculator

unittest

Here we can see that the Addition class has been inherited by the Calculator class, and we are able to add two or more numbers in a completely different class i.e. the calculator class. In this case, the Addition class is the Parent class while the Calculator class is the child class. This is especially useful as different classes can be created for each of the calculator functions of addition, subtraction, multiplication and division. And then all of these classes can be inherited by the Calculator class.

Polymorphism

The word polymorphism means having many forms. In programming, polymorphism means the same function name (but different signatures) being used for different types.
In Python, Polymorphism lets us define methods in the child class that have the same name as the methods in the parent class. In inheritance, the child class inherits the methods from the parent class.

Example of Polymorphism using the Multiplication Class in Calculator Program

unittest

Example of Polymorphism using the Subtraction Class in Calculator Program

unittest

Example of Polymorphism using the Division Class in Calculator Program

unittest

In all the classes i.e. Addition, Subtraction, Multiplication and Division we use the get_result method. The get_result method has been used by all the classes to perform different functions. We can thus see that the get_result function is taking multiple forms. This is the demonstration of Polymorphism in Calculator program.

Abstraction

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. An abstract method is a method that has a declaration but does not have an implementation. While we are designing large functional units we use an abstract class. When we want to provide a common interface for different implementations of a component, we use an abstract class.
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.

Example of Abstraction using the Calculation Class in Calculator Program

unittest

The Calculation class is an abstract base class itself. Here we have created an abstract method i.e. get_result(), which on its own does not give any result, but we saw that the get_result() method is used in the child classes Addition, Subtraction, Multiplication and Division.


The complete code can be accessed on Calculator Github