OOPs Concepts in Python

Ravi Kumar Banda
3 min readApr 14, 2020

Main goal of this article to give basic understanding of 3 Main OOPs concepts in Python. The 3 concepts are Inheritance, Polymorphism, Encapsulation.

Inheritance:

Def: Acquiring the properties of parent class to child class. There are different types of inheritance: simple , multiple, multilevel inheritance.

Simple Inheritance: One Base class and One Child class.

class BaseClass:
Body of base class
class DerivedClass(BaseClass):
Body of derived class

Multiple Inheritance: Def: If we have a scenario where there are two or more Base classes and 1 child class then this type of inheritance is called Multiple Inheritance.

class BaseClass1:
Body of base class
class BaseClass2:
Body of base class
.
.
class BaseClassN:
Body of base class
class DerivedClass(BaseClass1, BaseClass2,....,BaseClassN):
Body of derived class

Multi-Level Inheritance:Below picture can define the multi-level inheritance very well.

class Base:
pass

class Derived1(Base):
pass

class Derived2(Derived1):
pass

Polymorphism:

Def: One in many forms. One method but different actions/purposes. Polymorphism can be implemented using method Overloading, method Overriding, Operator Overloading. In fact, python does not support method overloading (but java, c++ support method overloading). But indirectly we can achieve this method overloading. See the code below.

class TestPolymorphism:
def add(self,a,b):
return (a+b)
def add(self,a,b,c):
return (a+b+c)
obj = TestPolymorphism()
print(obj.add(1,2)) #will get an error
print(obj.add(1,2,3))

But below code will work:

class TestPolymorphism:
def add(self,a,b,c=None):
if c ==None:
sum = a+b
return sum
else:
sum = a+b+c
return sum

obj = TestPolymorphism()
print(obj.add(1,2))
print(obj.add(1,2,3))

The first example will give an error as python does not support method overloading but in the second example we implemented it with the optional parameter. The best example for operator overloading in python is plus(+) operator. This operator can be used for addition and concatenation purposes. The “len()” is also an example of polymorphism. We can pass one or more strings, a list, dictionary.

Method Overriding: Def: When we have two or more methods having same prototype with different purposes then that is called method overriding. This can be implemented using inheritance concept. Look at below code:

class Parent:
def show():
print("In Parent")
class Child(Parent):
def show(self):
print("In Child")
obj = Child()
obj.show()

The above code will give the output as: In Child. If we want to call show() method from Parent class then we either use Parent.show() from Child class show() method or use the super().show()

class Parent:
def show():
print("In Parent")
class Child(Parent):
def show(self):
Parent.show() #or you can use super().show()
print("In Child")
obj = Child()
obj.show()

Encapsulation:

The idea here is to restrict direct access of data and methods. Wrapping the data(member variables) and methods as a single unit. “class” is a good example of encapsulation. We can demonstrate encapsulation concept using protected, private concepts of python.

Protected: Protected members(data) are the ones which are only accessed within the class or its subclass and can’t be access outside of the class. Protected members or methods are defined using single underscore(_).

class Parent:
def __init__(self):
self._a = "Parent"
class Child(Parent):
def __init__(self):
Parent.__init__(self)
print(self._a)
obj = Child()

Output will be “Parent”.

Private: private variables are similar to protected variable but only difference is that subclass does have access to private variables. Private variables are defined using double underscore “__”.

class Parent:
def __init__(self):
self.__a = "Parent"
class Child(Parent):
def __init__(self):
Parent.__init__(self)
print(self.__a)
obj = Child() # will give an error

The above code will give an error “AttributeError: ‘Child’ object has no attribute ‘_Child__a’”. As stated above that subclass does not have access to private variable of parent class. In order to access the private variable of parent class we need use member function of parent class.

class Parent:
def __init__(self):
self.__a = "Parent"
def show(self):
print(self.__a)
class Child(Parent):
def __init__(self):
Parent.__init__(self)
self.show()
obj = Child()

Now you can understand the real use-case for encapsulation : Consider a company with multiple departments. Consider payroll team and HR team. Payroll team can’t have access to HR team’s data and vice versa. If an HR wants some data of Payroll team then the access should go through member (method in class terminology) of payroll team.

I hope you have got some good understanding on Inheritance, Polymorphism, and Encapsulation concepts.

Please give a thumb up if you like the article and also provide your thoughts in the comment section.

--

--