OOPs Concept in Python

OOPs in Python: 

Object-oriented programming (OOPs) is a method of designing a program by binding there related properties and behaviours into individual objects. Here we will discuss, basics of object-oriented programming in Python.  

For instance, an object could represent a Student with properties like a name, age, class, marks, stream etc. 

Example:

  • Student->
    • Name 
    • Age 
    • roll number
    • stream
OOPs in Python

Principles of OOPs: 

  • Class 
  • Object
  • Method
  • Inheritance
  • Polymorphism
  • Data Abstraction
  • Encapsulation

Class: 

  • The class can be defined as a collection of objects. It is a logical entity that has some specific attributes and methods. 
  • We can also say that class is a blueprint of an object i.e. if any person is an object, class will have all the information about the structure or behaviour that person.  
  • OOPs, reduce the size of our code.
#python Program
#Rishikesh 
class Prepster
    def __init__(selfname , userid,password):
        self.name=name 
        self.userid=userid 
        self.password=password

Object: 

  • Object is a simple entity which posses some property or behaviour.
  • Everything in Python is an object, and almost everything has attributes and methods.
#python Program
#Rishikesh 
class Prepster
    def __init__(selfname , userid,password):
        self.name=name 
        self.userid=userid 
        self.password=password
 
ob1=Prepster(‘rishikesh’,‘Abc’,[email protected]##object 
print(ob1.name)

Output: 

rishikesh

Method: 

  • The method is a function that is associated with an object.
  • In Python, a method is not unique to class instances. Any object type can have methods.
#python Program
#Rishikesh 
class Prepster
    def __init__(selfname , userid,password):
        self.name=name 
        self.userid=userid 
        self.password=password
    def display(self): ##method of an class 
        print(self.name)
        print(self.userid)
        
ob1=Prepster(‘rishikesh’,‘Abc’,[email protected]##object 
ob1.display()

Output: 

rishikesh
Abc

Inheritance: 

  • Inheritance is the capability of one class to derive or inherit the properties from another class.
  • The benefits of inheritance are:
    • Reusability of code.
    • We can use the features of the other class, without modifying it.
    • It is transitive in nature i.e. A->B->C == A->C and B->C. 
  • The new class is known as derived class or child class.
oops in python : inheritance

Polymorphism:

  • Polymorphism contains two words “poly” and “morphs”. Poly means many, and morph means shape. 
  • Polymorphism states that we can perform a task in more than one ways. 
  • There are two type of Polymorphism:
    • Run-time Polymorphism  
    • Compile-Time Polymorphism
#python Program
#Rishikesh 
li=[1,2,3,4]
def add(lis):
    s=0
    for i in lis:
        s+=i 
    return s 
    
print(‘First Method’,add(li))
print(‘2nd Method:’,sum(li))

Output: 

First Method 10
2nd Method: 10

Data Abstraction: 

  • Data Abstraction is the process of hiding the real implementation of an application from a user and emphasizing only on usage of the application.
  • We need data abstraction, so that programmer can hide irrelevant data and increase its efficiency.

Encapsulation: 

  • It is a process of wrapping of data, variable, and methods into a single entity in a program. 
    • Class is an example that wraps all the data. 
  • It is also used to restrict the access of data, variables, and methods.