Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

OOP'S:- Object-Oriented Programming Structure, it is used to create a real-world based application using class and object.
OOP'S programming is based on Object because of we will define characteristics and behavior of Objects using class.

OOP provides better security, reusability, modularity, extendibility, usability, and accessibility features in real-world applications.


OOP provides individual memory allocation to individual objects for individual users.


Rule's of OOP'S:-

1 Class and Object:-

Class:-

It is user define datatype which is used to define the characteristics of an object using data member (variable) and member function(method).class is a blueprint of an object, we can define multiple objects definition underclass but all object should be related with each other.

class Classname:
    def methodname(self):
        statements
        statements
    ...
    ...

ref = Classname()
ref.methodname()

self is a keyword that is used to contain the address of the current object.


Object:-

It is a real-world entity that has identity, state, and behavior.  Identity provides the address of the object which will be unique.  Memory allocation of objects is used to specify the state, and calling of method will be defined behavior of the object.


"for example apple, orange these are the objects which will be defined by Fruit class".


if we want to create Object by python script:-

ref =  Classname()

ref = Student()


Example of Class and Object?

class Employee:
    def accept(self,empid,empname,job,salary):
        self.empid=empid
        self.empname=empname
        self.job=job
        self.salary=salary
    def display(self):
        print("ID is "+str(self.empid))
        print("name is "+str(self.empname))
        print("job is "+str(self.job))
        print("salary is "+str(self.salary))
obj = Employee()
obj.accept(1001,"XYZ","CLEARK",15000)
obj.display()
obj1 = Employee()
obj1.accept(1002,"MNO","MANAGER",25000)
obj1.display()
       
................................................................................................................................

Program to accept and display student record:-

class Student:
    def accept(self,rno,sname,branch,fees):
        self.rno=rno
        self.sname=sname
        self.branch=branch
        self.fees=fees

    def display(self):
        print("rno is "+str(self.rno) + " name is "+self.sname + " branch is " + self.branch +" fees is "+str(self.fees));

obj = Student()
obj.accept(1001,"xyz","CS",45000)
obj.display()

obj1 = Student()
obj1.accept(1002,"mno","IT",70000)
obj1.display()



Component of class:-

1 Data member or variable:-
  
It is used to define attributes of the object for example Employee object contains Empid, Empname, job, etc as an attribute of this, it will be declared as a variable.

We can define a data member using two different types:-

1.1)  class type or static:-  it will be declared underclass or static method

   class Classname:
       a=10  #class type



1.2) instance type or dynamic:- It will be declared by self keyword under dynamic method

  class Classname:
        def methodname(self):
           self.a=100   #instance type 


Program Solution:-

class Datamemberexample:
    a=100  #class type
    def fun(self):
        self.b=200    #reference type
        print(self.b)


obj =  Datamemberexample()
obj.fun()
print(Datamemberexample.a)  #class name
      

2 Member function or function:-

It is used to define data member of the class to implement data encapsulation

Type of member function

2.1 )  class type or static:-

It is used to define class type variables in the program.

 class Classname:
       def Methodname():
             statements
             statements


Classname.Methodname()

It has only a one-time memory allocation in the program because it will use Class memory of the program.

Example of Static:-
class StaticFunction:
       def fun():
           a=int(input("enter first number"))
           b=int(input("enter second number"))
           c=a+b
           print(c)

StaticFunction.fun()
StaticFunction.fun()


2.2) Instance type or dynamic:-

this type of member function will be declared using self keyword and called by object .it will be allocated individual memory for the different objects because it has a unique address for Object.
 

   class DynamicFunction:
       def fun(self):
           a=int(input("enter first number"))
           b=int(input("enter second number"))
           c=a+b
           print(c)

obj=DynamicFunction()
obj.fun()
obj1=DynamicFunction()
obj1.fun()

 3 Constructor or Init Method:-

   It is a special component of the class that will be called when we create an object.

class Consdemo:
      def __init__(self):
          statements



Type of Constructor?

1 Single parameter:-

  we can pass only self as a parameter to single parameter constructor

class A:
    def __init__(self):
        statements

2  Multiple Parameter:-

  We can pass multiple values as an argument to multiple parameter constructor.

class A:
   def __init__(self,a,b):
       self.a=10
       self.b=20



Note:  we should create only one type of constructor in the program because only one constructor will be called.

The constructor is only for data member initialization not for functionality.

4 Destructor or Delete Method:-

This method will be called when we destroy or delete the object
It is used to remove dynamic memory space in the program.
this block by default created by class, if we want to implement any functionality then we can manually re-create destructor.



Complete Program Explanation of Constructor and Destructor:-

class Addition:
    def __init__(self):
        self.a=100
        self.b=200
    def __init__(self,a,b):
        self.a=a
        self.b=b
    def add(self):
        print("result is "+str(self.a+self.b))
    def __del__(self):
        print("Destructor will be called")

obj = Addition(10,20)
obj.add()

del obj


Data Abstraction and Data Encapsulation:-

  Data abstraction:-
  To show essential details and hide non-essential details in the program is managed by data abstraction.

  it is used to provide data hiding from end-users in an application.

  It is used to implement security and accessibility features in the program.

  How we implement Data abstraction practically:-

  we will use access specifier, python support access specifier using the symbol


  if we use  __   private   (accessed in the same class, means it can not access outside of the class)
  if we use  _    protected (accessed from base class to derived class )
  if we use        without symbol then public (accessed in all classes by default python member function is public)

class Classname:
    def __fun(self):
        print("Private")
    def _fun2(self):
       print("Protected")
    def fun3(self):
      print("public")



Example of Data abstraction in python:-

class A:
  def __fun(self):
      print("Private")
  def _fun2(self):
      print("Protected")
  def fun3(self):
      self.__fun()
      print("public")

obj = A()
obj.fun3()



  Data Encapsulation:-

It is used to bind all the details using a single unit. if we declare data members under member function then it is called data encapsulation. we will use a data binding principal to implement encapsulation functionality.
 In python script we can not access any instance type data member directly .instance type variable will be defined by self keyword under member function.

class A:

  a=1000  #class type
  def fun(self):
   a=10   #local instance type
   print(A.a)  #call class type
   print(a)    #call local instance type
   self.a=100
  def fun1(self):
    print(self.a)  #call self type
    print(A.a)     #call class type


obj = A()
obj.fun()
obj.fun1()





3 Polymorphism:-

Poly means many and morph-ism means forms, using this we will create the same name method or operator which will be implemented into multiple forms.

function overloading, operator overloading and function overriding is the practical example of Polymorphism.

Function Overloading is not managed by python script because we can define multiple functions but call only one function at a time.


Operator overloading:-  using this we can reload or redefine operator definition.  A single operator can be used in multiple forms using an operator overloading concept.

python use predefined method to implement operator definition:-

1 function  __add__(self):
       statements

2 function  __sub__(self):
       statements

3 function  __mul__(self):
      statements

4  function __truediv__(self):
       statements

5   function __floordiv__(self):
         statements


Operator overloading example in Python using + Operator:-

class Ope:
    def __init__(self,num):
        self.num=num
    def __add__(self,other):
        return Ope(self.num*other.num)
    def __str__(self):  #TO RETURN VALUE OF CURRENT OBJECT WHEN WE PRINT(OBJ) THEN IT CALL
        return "result is "+str(self.num)


obj = Ope(100)
obj1 = Ope(200)
obj3 = obj + obj1   #__add__(obj,obj1)
print(obj3)


4 Inheritance:-  



It is used to provide re-usability means we can adapt the features of base class to the derived class.

Type of Inheritance:-

4.1) Single  Inheritance:-

From Base Class to Derived Class

class A:
    methods

class B(A):
    methods

4.2) Multilevel  Inheritance:-

From base class to derived class and derived class to sub derived class.
class A:
    methods
class B(A):
    methods
class C(B):
   methods


4.3) Hierarchical  Inheritance:-

 One base class to different child class

class A:
   methods

class B(A):
   methods

class C(A):
    methods
4.4)  Multiple Inheritance:-

 Two base classes in one child class

class A:
     methods

class B:
    methods

class C(A, B):
    methods

   


Example of Single Inheritance:-

class Admin:
    def accept(self,id,name):
        self.id=id
        self.name=name

    def display(self):
        print("ID is "+str(self.id)+" name is "+str(self.name))

class Employee(Admin):
    def accept1(self,sal):
        self.sal=sal
    def display1(self):
        print("Salary is "+str(self.sal))

print("Employee Information")
obj = Employee()
obj.accept(1001,"emp")
obj.accept1(12000)
obj.display()
obj.display1()

print("Admin Info")
obj1 = Admin()
obj1.accept(1000,"admin")

obj1.display()

Example of Multilevel Inheritance:-

class Admin:
    def accept(self,id,name):
        self.id=id
        self.name=name

    def display(self):
        print("ID is "+str(self.id)+" name is "+str(self.name))

class Employee(Admin):
    def accept1(self,sal):
        self.sal=sal
    def display1(self):
        print("Salary is "+str(self.sal))
class Otherstaff(Employee):
    def accept2(self,bonus):
        self.bonus=bonus
    def display2(self):
        print("Bonus is "+str(self.bonus))

print("Employee Information")
obj = Employee()
obj.accept(1001,"emp")
obj.accept1(12000)
obj.display()
obj.display1()

print("Admin Info")
obj1 = Admin()
obj1.accept(1000,"admin")
obj1.display()

print("Other Staff Info")
obj2 = Otherstaff()
obj2.accept(1002,"otherstaff")
obj2.accept1(12000)
obj2.accept2(200)
obj2.display()
obj2.display1()

obj2.display2()


Example of Hierarchical Inheritance:-

class Admin:
    def accept(self,id,name):
        self.id=id
        self.name=name

    def display(self):
        print("ID is "+str(self.id)+" name is "+str(self.name))

class Employee(Admin):
    def accept1(self,sal):
        self.sal=sal
    def display1(self):
        print("Salary is "+str(self.sal))
class Otherstaff(Admin):
    def accept2(self,bonus):
        self.bonus=bonus
    def display2(self):
        print("Bonus is "+str(self.bonus))

print("Employee Information")
obj = Employee()
obj.accept(1001,"emp")
obj.accept1(12000)
obj.display()
obj.display1()

print("Admin Info")
obj1 = Admin()
obj1.accept(1000,"admin")
obj1.display()

print("Other Staff Info")
obj2 = Otherstaff()
obj2.accept(1002,"otherstaff")
#obj2.accept1(12000)
obj2.accept2(200)
obj2.display()
#obj2.display1()

obj2.display2()
..................................................................................

Example of Multilevel Inheritance:-
class Admin:
    def fun(self):
        print("Admin")
    def accept(self,id,name):
        self.id=id
        self.name=name

    def display(self):
        print("ID is "+str(self.id)+" name is "+str(self.name))

class Employee:
    def fun(self):
        print("Employee")
    def accept1(self,sal):
        self.sal=sal
    def display1(self):
        print("Salary is "+str(self.sal))
class Otherstaff(Admin,Employee):
    def accept2(self,bonus):
        self.bonus=bonus
    def display2(self):
        print("Bonus is "+str(self.bonus))

print("Employee Information")
obj = Employee()
#obj.accept(1001,"emp")
obj.accept1(12000)
#obj.display()
obj.display1()

print("Admin Info")
obj1 = Admin()
obj1.accept(1000,"admin")
obj1.display()

print("Other Staff Info")
obj2 = Otherstaff()
obj2.fun()
obj2.accept(1002,"otherstaff")
obj2.accept1(12000)
obj2.accept2(200)
obj2.display()
obj2.display1()

obj2.display2()



.....................................................................................................................................................

Inheritance Example for Area of the circle, triangle, and rectangle:-

class Circle:
    def accept(self,param1):
        self.param1=param1
    def areaofcirlce(self):
        self.area = 3.14*self.param1*self.param1
    def display(self):
        print("Area  is "+str(self.area))
class Triangle(Circle):
    def accept1(self,param2):
        self.param2=param2
    def areaoftriangle(self):
        self.area = (self.param1*self.param2)/2
class Rect(Triangle):
    def areaofrectangle(self):
        self.area = (self.param1*self.param2)
print("Area of the circle")
obj = Circle()
obj.accept(12)
obj.areaofcirlce()
obj.display()
print("Area of Triangle")
obj1 =Triangle()
obj1.accept(12)
obj1.accept1(2)
obj1.areaoftriangle()
obj1.display()
print("Area of Rectangle")
obj2 =Rect()
obj2.accept(12)
obj2.accept1(2)
obj2.areaofrectangle()
obj2.display()
   

Function Overriding:-

we can create the same name method from base class to derived class only functionality will be different.

no need to use any keyword for function overriding, it will be implemented automatically.

class A:
    def fun(self):
        print("A")

class B(A):
    def fun(self):
        print("B")

        
obj = B()
obj.fun()
    









Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post