OOPs concepts in Python: Python Classes and Objects, Inheritance, Overloading, Overriding and Data hiding
In the previous tutorial we some of the Input/output operations that Python provides.
We came to know how to use these functions to read the data from the user or from the external sources and also how to write those data into external sources. Also, we learned how to divide a huge code into smaller methods using functions and how to call or access them.
Further Reading => Explicit Range of Free Python Training Tutorials
In this tutorial, we will discuss the Advanced Python concept called the OOPs and different types of oops concepts that are available in Python and how and where to use them.
What You Will Learn:
Watch the VIDEO Tutorials
Video #1: Class, Objects & Constructor in Python
Video #2: Concept of Inheritance in Python
Video #3: Overloading, Overriding & Data Hiding in Python
Classes and Objects
- Python is an object-oriented programming language where programming stresses more on objects.
- Almost everything in Python is objects.
Class in Python is a collection of objects, we can think of a class as a blueprint or sketch or prototype. It contains all the details of an object.
In the real-world example, Animal is a class, because we have different kinds of Animals in the world and all of these are belongs to a class called Animal.
Defining a class
In Python, we should define a class using the keyword ‘class’.
class classname: #Collection of statements or functions or classes
class MyClass: a = 10 b = 20 def add(): sum = a+b print(sum)
In the above example, we have declared the class called ‘Myclass’ and we have declared and defined some variables and functions respectively.
To access those functions or variables present inside the class, we can use the class name by creating an object of it.
First, let’s see how to access those using class name.
class MyClass: a = 10 b = 20 #Accessing variable present inside MyClass print(MyClass.a)
An object is usually an instance of a class. It is used to access everything present inside the class.
Creating an Object
variablename = classname
ob = MyClass()
This will create a new instance object named ‘ob’. Using this object name we can access all the attributes present inside the class MyClass.
class MyClass: a = 10 b = 20 def add(self): sum = self.a + self.b print(sum) #Creating an object of class MyClass ob = MyClass() #Accessing function and variables present inside MyClass using the object print(ob.a) print(ob.b) ob.add()
Constructor in Python
Constructor in Python is a special method which is used to initialize the members of a class during run-time when an object is created.
In Python, we have some special built-in class methods which start with a double underscore (__) and they have a special meaning in Python.
The name of the constructor will always be __init__().
Every class must have a constructor, even if you don’t create a constructor explicitly it will create a default constructor by itself.
class MyClass: sum = 0 def __init__ (self, a, b): self.sum = a+b def printSum(self): print(“Sum of a and b is: ”, self.sum) #Creating an object of class MyClass ob = MyClass(12, 15) ob.printSum()
Sum of a and b is: 27
If we observe in the above example, we are not calling the __init__() method, because it will be called automatically when we create an object to that class and initialize the data members if any.
Always remember that a constructor will never return any values, hence it does not contain any return statements.
Inheritance is one of the most powerful concepts of OOPs.
A class which inherits the properties of another class is called Inheritance.
The class which inherits the properties is called child class/subclass and the class from which properties are inherited is called parent class/base class.
Python provides three types of Inheritance:
- Single Inheritance
- Multilevel Inheritance
- Multiple Inheritance
#1) Single Inheritance
In Single inheritance, one class will inherit the properties of one class only.
class Operations: a = 10 b = 20 def add(self): sum = self.a + self.b print(“Sum of a and b is: “, sum) class MyClass(Operations): c = 50 d = 10 def sub(self): sub = self.c – self.d print(“Subtraction of c and d is: ”, sub) ob = MyClass() ob.add() ob.sub()
Sum of a and b is: 30
Subtraction of c and d is: 40
In the above example, we are inheriting the properties of the ‘Operations’ class into the class ‘MyClass’.
Hence, we can access all the methods or statements present in the ‘Operations’ class by using the MyClass objects.
#2) Multilevel Inheritance
In multilevel Inheritance, one or more class act as a base class.
Which means the second class will inherit the properties of the first class and the third class will inherit the properties of the second class. So the second class will act as both the Parent class as well as Child class.
class Addition: a = 10 b = 20 def add(self): sum = self.a + self.b print(“Sum of a and b is: ”, sum) class Subtraction(Addition): def sub(self): sub = self.b-self.a print(“Subtraction of a and b is: ”, sub) class Multiplication(Subtraction): def mul(self): multi = self.a * self.b print(“Multiplication of a and b is: ”, multi) ob = Multiplication () ob.add() ob.sub() ob.mul()
Sum of a and b is: 30
Subtraction of a and b is: 10
Multiplication of a and b is: 200
In the above example, class ‘Subtraction’ inherits the properties of class ‘Addition’ and class ‘Multiplication’ will inherit the properties of class ‘Subtraction’. So class ‘Subtraction’ will act as both Base class and derived class.
#3) Multiple Inheritance
The class which inherits the properties of multiple classes is called Multiple Inheritance.
class Addition: a = 10 b = 20 def add(self): sum = self. a+ self.b print(“Sum of a and b is: “, sum) class Subtraction(): c = 50 d = 10 def sub(self): sub = self.c-self.d print(“Subtraction of c and d is: ”, sub) class Multiplication(Addition,Subtraction): def mul(self): multi = self.a * self.c print(“Multiplication of a and c is: ”, multi) ob = Multiplication () ob.add() ob.sub() ob.mul()
Sum of a and b is: 30
Subtraction of c and d is: 10
Multiplication of a and c is: 500
Method Overloading in Python
Multiple methods with the same name but with a different type of parameter or a different number of parameters is called Method overloading
def product(a, b): p = a*b print(p) def product(a, b, c): p = a*b*c print(p) #Gives you an error saying one more argument is missing as it updated to the second function #product(2, 3) product(2, 3, 5)
Method overloading is not supported in Python, because if we see in the above example we have defined two functions with the same name ‘product’ but with a different number of parameters.
But in Python, the latest defined will get updated, hence the function product(a,b) will become useless.
Method Overriding in Python
If a subclass method has the same name which is declared in the superclass method then it is called Method overriding
To achieve method overriding we must use inheritance.
class A: def sayHi(): print(“I am in A”) class B(A): def sayHi(): print(“I am in B”) ob = B() ob.sayHi()
I am in B
Data Hiding in Python
Data hiding means making the data private so that it will not be accessible to the other class members. It can be accessed only in the class where it is declared.
In python, if we want to hide the variable, then we need to write double underscore (__) before the variable name.
Class MyClass: __num = 10 def add(self, a): sum = self.__num + a print(sum) ob = MyClass() ob.add(20) print(ob.__num) #The above statement gives an error because we are trying to access private variable outside the class
Traceback (most recent call last):
File “DataHiding.py”, line 10, in
AttributeError: MyClass instance has
no attribute ‘__num
The class is a blueprint or template which contains all the details of an object, where the object is an instance of a class.
- If we want to get the properties of another class into a class, then this can be achieved by inheritance.
- Inheritance is of 3 types- Single Inheritance, Multilevel Inheritance, and Multiple Inheritance.
- Method overloading is not supported in Python.
- Method overriding is used to override the implementation of the same function which is defined in another class.
- We can make the data attributes as private or hide them so that it will not be accessible outside the class where it is defined.
Our upcoming tutorial will explain more about Additional Python concepts in detail!!