Skip to main content

Oops Concept in Python

In our previous Python tutorial, we have explained about Data types in Python. In this tutorial, we will discuss about Oops Contepts in Python.

The object-oriented concepts are mainly focuses on to write the reusable code. It design the program using classes and objects. The object is related to real-world entities such as car, bike, computer etc.

Just like any other general-purpose programming language, Python is also an object-oriented language. It allows to develop applications using object-oriented approach by creating classes and objects.

So here in this tutorial, we will discuss different types of oops concepts that are available in Python and how and where to use them.

Class

A class in Python is defined as a collection of objects. It’s like an object constructor or blueprint for creating objects. The class contains specific details such as attributes and methods.

The real-world example, Car is a class as there are different kinds of Cars in the world but all belongs to class Car.

The keyword class used to create a class:

class Car:
 name = 'Ford'
 model = '2022'

Object

Everything in Python is object. An object is usually an instance of a class and can be used to access everything present in the class.

When a class is defined, it needs to create an object of that class to allocate the memory:

class Car:
 name = 'Ford'
 model = '2022'
  
carObj = Car()
print(carObj.name)
print(carObj.model)

Output:

Ford
2022

In above example, defined class Car with two attributes name and model.

Constructor

In Python, constructor is a specific mehtod that is used to initialize the members of a class during runtime when object is created.

Python has built-in function __init__() used as a name of constructor. The function __init__() is executed automatically when the class is being initiated to assign values to object properties.

class Book:
 def __init__(self, name, author):
    self.name = name
    self.author = author

 def displayBookDetails(self):
  print('Book Name: '+self.name+' , Author: '+self.author) 
  
bookObj = Book('Clean Code', 'Robert C. Martin')
bookObj.displayBookDetails()

In above example code, the function __init__() called automatically when create an object of class Book and initialize the data members if any.

Method

The methods are functions defined in a class. The methods are accessed using class object. The method in Python is not unique to class instances, any object type can have methods.

class Car:
 def __init__(self, name, model):
    self.name = name
    self.model = model

 def getName(self):
  print(self.name) 
  
 def getModel(self):
  print(self.model) 
  
carObj = Car('Maruti', '2021')
carObj.getName()
carObj.getModel()

Output:

Maruti
2021

In above example code, In class Car defined method getName() and getModel(). Created object carObj of class Car and accessing methods using object.

We have also used built-in __init__() function which is always executed when the class is being initiated to assign values to object properties. So here in above exanple, we are passing constructor values while creating object.

Inheritance

Inheritance is an imprtant aspect of object-oriented programming. It allow us simulate real-world concept of inheritance.

Inheritance allows us define a parent class that inherits all the methods and properties of a child class.

The class that inherits the properties is called child class and the class from which properties are inherited is called parent class or base class.

Here first we will create a parent/base class:

class Person:
 def __init__(self, name, city):
    self.name = name
    self.city = city

 def displayDetails(self):
  print(self.name, self.city) 
  
   
personObj = Person('Adam', 'Newyork')
personObj.displayDetails()

now we will create a child class student that inherits the functionality from another class. We will send the parent class Person as a parameter when creating the child class to inherit it:

class Person:
 def __init__(self, name, city):
    self.name = name
    self.city = city

 def displayDetails(self):
  print(self.name, self.city) 
  
class Student(Person):
  pass

studentObj = Student('William', 'London')
studentObj.displayDetails()

In above example code, create a child class Student and passed class Persona as parameter to inherit it. Now we can call the method of inherited class using child class.

We can use the pass keyword in class when we do not want to add any other properties or methods to the class.

Data Hiding in Python

Data hiding is an important aspect of object-oriented programming. It’a allows to make data private so that it will not be accessible to the other class members. It’s only accessible in the class where it is declared.

In Python, we need to declare the variable by writting double underscore (__) before the variable name to make the variable private.

class Calculate:
    __num = 10
    def add(self, value):
           sum = self.__num + value
           print(sum)           
 
obj = Calculate()
obj.add(20)

print(obj.__num)

In above example code, private declared with by writting double underscore (__) before the variable name. The variable call within class work without any error. But when accessed outside class, it will return error.