Python Object Oriented Programming

Object-oriented programming is a programming paradigm that uses objects, and classes in programming. It helps to implement real-world concepts like(polymorphism, encapsulation, inheritance, etc). It also solves real-world problems using these entities. The main concept is to combine the functions and data, and they work together, such that they can not be accessed outside of the particular class. 

Some Important Terms in OOPS – 

  • Class 
  • Objects 
  • Inheritance 
  • Polymorphism 
  • Encapsulation 
  • Abstraction 

Class – 

It is a collection of objects containing a blueprint of what it is made up of. It contains some attributes, functions, methods, or variables. 

Why do we need a Class? 

Let’s say, you want to keep track of an employee and track his department and salary. You can do this task easily by taking two variables. But imagine handling the same data of 1000 employees or more. If you plan to use a list, then how would you know which data belongs to which employee? 

Here class comes into play. We can create a class, having department and salary as two of its attributes. And whenever we want to store the data of an employee, then we can just instantiate the object of that class, and we can just store any amount of data. 

  • Classes in Python are created by using the keyword class. 
  • Attributes are variables belonging to a class. 
  • Attributes are always public and are accessible using the dot(.) operator.

Syntax – 

class class_name: 
#Statements

Example – 

class employee: 
department #creating a variable for department 
salary #creating a variable for salary 

Objects – 

Objects are like real-world objects having some attributes, behavior, and identity. The objects are the center of object-oriented programming. An object represents a copy of a class. The objects are used to represent the data. 

Syntax:

class_name obj_name

Code – 

Python Code

class employee: 
    def __init__(self , post , name): #constructor(initialises the newly created object of the class) 
        self.post = post
        self.name = name 
e1 = employee('manager' , 'XYZ') #created an object of class employee and it contains all the attributes of the class employee , the object e1 , will have its department and salary as well. 

Self – 

The self parameter is a reference to the current instance of the class and is used to access the attributes and methods in the class itself. Self is similar to the “this” keyword in C++. 

Inheritance – 

The property of one class to inherit/derive the properties from another class. The class that inherits the properties of another class is called a derived class. The class, whose properties are inherited, is called a parent class or base class. 

  • It makes the code reusable. It allows the use of the attributes of the parent class in the child class by inheriting it and no need to write the same code again and again.
  • If class B is inheriting from class A, then all the subclasses of B will automatically inherit from Class A. 
  • It also represents real-world problems.

Code:

Python Code

class Human: #class human 
    def __init__(self , name , idnumber): #constructor (initialises the newly created 
    #object of the class) 
        self.name = name 
        self.idnumber = idnumber 

class employee(Human): #class employee havinf some of its attributes inherited from 
#Human class 
    def __init__(self , name , idnumber , salary, post): #constructor 
        self.salary = salary 
        self.post = post 
        Human.__init__(self , name , idnumber) 

    def details(self): #function to print the details of the employee 
        print("My name is : {}".format(self.name)) 
        print("the id is :{} ".format(self.idnumber)) 
        print("post : {}".format(self.post)) 

e1 = employee('XYZ' , 89367 , 60000 , 'manager')#created an object of class employee

e1.details() 

Output:

My name is: XYZ
the id is:89367
post: manager

We have two classes: Human (parent class) and Employee (Child Class). The Human class is inherited by the Employee class. The methods of the Human class can be accessed via the employee class. The details() function demonstrates how a child class can modify the behavior of the parent class. 

PolyMorphism – 

It means having different forms. This means the same function is used for different types.

Code:

Python Code

def product(a , b , c = 4): 
    return a*b*c 
print(product(2 , 3)) 
print(product(4 , 5 ,6)) 

Output:

24
120

Here, the same function product is used in different cases.

Encapsulation – 

The concept of combining data with methods is called Encapsulation. This also restricts the access of variables in a class and prevents accidental modification of the data. To prevent change in data, we can make use of private, protected, and public data member functions. By default, all the data members are public in python. 

If we make the data members private, its data members and functions are inaccessible outside that class. 

We can also use protected if we want our data members to be protected and can be accessed through the use of friend class concepts. 

Code:

Python Code

class employee: 
# constructor 
    def __init__(self, name, salary, project): 
# data members 
        self.name = name 
        self.salary = salary 
        self.project = project 
# method 
# to display employee's details 
    def display(self): 
# accessing public data member 
        print("Name:", self.name, ', Salary:', self.salary) 
# method 
    def Work(self): 
        print(self.name, 'is working on', self.project) 
# creating object of a class 
e1 = employee('Jessa', 8000, 'NLP') 
# calling public method of the class 
e1.display() 
e1.Work() 

Output:

Name: Jessa, Salary: 8000
Jessa is working on NLP

Special thanks to Ishita Dhiman for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this articleIf you want to suggest any improvement/correction in this article please mail us at [email protected]