oops concepts in python with real life example

By

  •  OOP(object oriented programming) is used to solve the problems with the help of  classes and objects.
  • Python is an object oriented programming language like c++,java and c#.
  • We can easily develop the applications using this object oriented approach.
  • An object oriented paradigm is used to design the program using using classes and objects.
  • This oops concepts in python focuses on creating reusable code.
There are some major concepts is used in oops(object -oriented programming system) as given below.
  1. class 
  2. object
  3. method
  4. Inheritance
  5. Polymorphism
  6. Encapsulation
  7. Abstraction
1.) Class:
  • A class is a blue print of an object.
  • A class is a collection of data and methods
e.g.
class student:
    a=5
    b=6
    s=a+b
    def show_data(self):
      print("Value of sum is= ",self.s)
s1=student()
s1.show_data()
Output:-
Value of sum is=  11
>>> 

2.) Object:-
  • Every thing is an object in python.
  • Object is an instance of a class .Object is an entity that has some state and behavior.
  • We can access the class data with the help of object(instance) of the class.
More details... 
e.g.
class employee:
   def __init__(self,a,b,c):
       self.id=a
       self.name=b
       self.salary=c
   def display(self):
       print("Employee id is= ",self.id)
       print("Employee name is= ",self.name)
       print("Employee salary is= ",self.salary)
#create the object/instance of the class       
e1=employee(102,'Ram',30000)
e1.display()
Output:-
Employee id is=  102
Employee name is=  Ram
Employee salary is=  30000
>>> 
3.) Method:-
  • Method is a function which is define inside the body of the class.
  • Methods are used to define and manipulate  the behaviours of an objects.
e.g.
class student:
    def __init__(self,a,b,c):
        self.id=a
        self.name=b
        self.school=c
    def show(self):
        print("student name is :",self.name)
    def display(self):
        print("Student id is: ",self.id)
        print("student school is: ",self.school)
s1=student(102,'Mohan',"Delhi public school")
s1.show()
s1.display()
Output:-
student name is : Mohan
Student id is:  102
student school is:  Delhi public school
>>> 
Descriptions:-
  • Here i have called show() method first,which is responsible to display the name of the student.
  • After that i have called display() method,which is responsible to display the student id and school name. 
4.) Inheritance:-
  • Inheritance is the most important concept of object-oriented programming  language.It simulates the real world concepts of inheritance.
  • In inheritance, a new class called derived class can be created to inherit features of an existing class called base class.
  • Base class is also known as super class or parent class.
  • Derived class is also know as sub class or child class.
  • In inheritance,when we create the object of child class.Then this child class object acquires all the properties and behaviors of the parent object. 
  • Inheritance concept provide the re-usability of code.
e.g.
#parent class
class student:
    def __init__(self):
        print("I am parent class constructor")
    def display(self):
         print("I am parent class display method")
    def calculate(self,a,b,c):
        self.a=a
        self.b=b
        self.c=c
        self.s=a+b+c
        print("Sum of three numbers is= ",self.s)        
#child class
class student_child(student):
     def __init__(self):
         #call the super function
         super().__init__()
         print("I am child class constructor")
     def show(self):
         print("I am show method of child class")
#creating the instance of the child/derived class
x=int(input("Enter first number: "))
y=int(input("Enter second number: "))
z=int(input("Enter third number: "))
c1=student_child()
c1.display()
c1.calculate(x,y,z)
c1.show()
Output:-
Enter first number: 20
Enter second number: 40
Enter third number: 50
I am parent class constructor
I am child class constructor
I am parent class display method
Sum of three numbers is=  110
I am show method of child class
>>> 
Descriptions:-
Here i have created the object of the student_child class.All method of student class (parent class) and student_child(derived) class will be loaded in main memory.Because the student_child class inherited the all features of student class.
Watch Inheritance complete Lecture 38 Video:-

5.) Polymorphism:-
'Polymorphism' word  is made two word "poly" and  "morph".'poly' means many and 'morph' means shape/forms.In this concept we can perform one task in different ways for example,suppose we want to go 'Delhi ',we generally uses 'GPS' for navigating the routes.Here GPS located different routes for same destination Delhi.In programming, this is called 'polymorphism'.
  • In oop concept.one-task can be performed in several different ways.
Polymorphism is two types:-
  1. compile-time polymorphism(method overloading)
  2. Runtime polymorphism(method overriding)
We will learn it in detail in a separate lecture.More details...
e.g. 
class student:
    def name(self):
        print("My good name is Ram")
    def display(self):
        print("I am a good student")
class employee:
    def name(self):
        print("My name is Rajesh chauhan")
    def display(self):
        print("I am not a good employee")
#common Interfacefunction
def func_name(object):
    object.name()
    object.display()
#creating the instace/object of the class
s1=student()
e1=employee()
#Passing these class objects in common interface function one by one and get output
func_name(s1)
func_name(e1)
Output:-
My good name is Ram
I am a good student
My name is Rajesh chauhan
I am not a good employee
>>> 
Watch polymorphism complete video:-

6.) Encapsulation:-
  • Encapsulation is a important concept of object oriented programming language.
  • In encapsulation methodology we can restrict access to methods and variables.
  • To prevent the data access of the class from out side or same class, is known as encapsulation.
  • In python ,we denote private attributes using prefix underscore(single(_) or double(__)).
e.g.
class employee:
    def __init__(self):
        self._salary=2000
        self.name='Ram'
        self.age=30
    def show(self):
        print("salary of the employee",self._salary)
    def display_data(self):
        print("Employee name is:",self.name)
        print("Employee name is:",self.age)
    def change_salary(self,sal):
        self._salary=sal
#creating the instance/object of the employee class
e1=employee()
e1.show()
e1.display_data()
e1._salary=50000
e1.show()
Output:-
salary of the employee 2000
Employee name is: Ram
Employee name is: 30
salary of the employee 50000
>>> 
7.) Abstraction:-
  • Abstraction is used to hide internal details and show only functionalities.
  • A class from which an object can not be created is called an abstract class.
e.g.
from abc import ABC,abstractmethod
class shape(ABC):
    @abstractmethod
    def draw(self):
        pass
class circle(shape):
    def draw(self):
        print("Draw the circle")
class square(shape):
    def draw(self):
        print("Draw the square")
#s=shape()  --> this will show error because shape is abstract class
c=circle()
c.draw()

Output:-
Draw the circle
>>> 
Description:-
  • abc stands for abstract base classes.To create an abstract class we need to derive it from class ABC present in abc module.
  • Now we need to mark draw() as abstract method using the decorator @abstractmethod
  • If an abstract class contains only methods marked by the decorator @abstractmethod,it is called interface.
Watch complete lecture video:-
----------------coming soon-----------------
For More...

1 comment:

  1. A big thanks for sharing this post by the way if anyone looking for Best Consulting Firm for Fake Experience Certificate Providers in hyderabad, India with Complete Documents So Dreamsoft Consultancy is the Best Place.Further Details Here- 9599119376 or VisitWebsite-https://experiencecertificates.com/experience-certificate-provider-in-Hyderabad.html

    ReplyDelete

Powered by Blogger.