0% found this document useful (0 votes)
60 views

Oops Concepts

The document discusses various object-oriented programming concepts in Python including classes, objects, inheritance, polymorphism, encapsulation, and abstraction. It provides examples of how to define classes and objects in Python, use inheritance and polymorphism through method overriding and operator overloading. The key differences between method overloading and overriding are also explained.

Uploaded by

RedHat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Oops Concepts

The document discusses various object-oriented programming concepts in Python including classes, objects, inheritance, polymorphism, encapsulation, and abstraction. It provides examples of how to define classes and objects in Python, use inheritance and polymorphism through method overriding and operator overloading. The key differences between method overloading and overriding are also explained.

Uploaded by

RedHat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

OOPS CONCEPS

PYTHON OOPS CONCEPTS


 Python is an object-oriented programming language.
 It allows us to develop applications using an Object Oriented approach.
 In Python, we can easily create and use classes and objects.
 Major principles of object-oriented programming system are given below.
 Object
 Class
 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.
 For example: if you have an employee class then it should contain an attribute and method, i.e.
an email id, name, age, salary, etc.
 Create a Class
 To create a class, use the keyword class:
 class MyClass:
 <statement-1>   
         .   
         .    
 <statement-N>
# Class for Computer Science Student
class CSStudent:
    stream = 'cse'                  # Class Variable
    def __init__(self,name,roll):
        self.name = name            # Instance Variable
        self.roll = roll            # Instance Variable
  
# Objects of CSStudent class
a = CSStudent('Geek', 1)
b = CSStudent('Nerd', 2)
  
print(a.stream)  # prints "cse"
print(b.stream)  # prints "cse"
print(a.name)    # prints "Geek"
print(b.name)    # prints "Nerd"
print(a.roll)    # prints "1"
print(b.roll)    # prints "2"
  
# Class variables can be accessed using class name also
print(CSStudent.stream) # prints "cse"
 OBJECT
 The object is an entity that has state and behavior. It may be any real-world object like the
mouse, keyboard, chair, table, pen, etc.
 Now we can use the class named myClass to create objects
 class MyClass:
 x=5
 p1 = MyClass()
 print(p1.x) OUTPUT:- 5
 __init__() Function
 All classes have a function called __init__(), which is always executed when the class is being
initiated.
 Use the __init__() function to assign values to object properties, or other operations that are
necessary to do when the object is being created.
 Also known as “dunder method”
 Create a class named Person, use the __init__() function to assign values for name and age
 class Person:
  def __init__(self, name, age):
    self.name = name
    self.age = age

p1 = Person("John", 36)

print(p1.name)
print(p1.age) OUTPUT:- John
36
 the self is used as a reference variable which refers to the current class object. It is always the
first argument in the function definition. However, using self is optional in the function call.
 Note: The __init__() function is called automatically every time the class is being used to
create a new object.
 class Employee:  
     id = 10;  
     name = "John"  
     def display (self):  
         print("ID: %d \nName: %s"%(self.id,self.name))  
 emp = Employee()  
 emp.display()
 OUTPUT:- ID: 10
Name: ayush
 Polymorphism

 The word polymorphism means having many forms. ... Real life example of polymorphism: A

person at the same time can have different characteristic. Like a man at the same time is a father,
a husband, an employee. So the same person posses different behavior in different situations.

 Example of inbuilt polymorphic functions :

 # len() being used for a string

 print(len(“hello"))

 # len() being used for a list

 print(len([10, 20, 30]))

 OUTPUT:- 5

3
 Examples of used defined polymorphic functions :

 def add(x, y, z = 0):

 return x + y+z

 print(add(2, 3))

 print(add(2, 3, 4))

 OUTPUT:- 5

9
 Polymorphism with class methods:
 class India():
 def capital(self):
 print("New Delhi is the capital of India.")

 def language(self):
 print("Hindi the primary language of India.")

 def type(self):
 print("India is a developing country.")
 class USA():
 def capital(self):
 print("Washington, D.C. is the capital of USA.")

 def language(self):
 print("English is the primary language of USA.")

 def type(self):
 print("USA is a developed country.")
 obj_ind = India()
 obj_usa = USA()
 for country in (obj_ind, obj_usa): [obj.ind.capital()]
 country.capital() [obj.usa.capital()]
 country.language()
 country.type()
 OUTPUT:- New Delhi is the capital of India.
Hindi the primary language of India.
India is a developing country.
Washington, D.C. is the capital of USA.
English is the primary language of USA.
USA is a developed country.
 Polymorphism with Inheritance:
 class Bird:
 def intro(self):
 print("There are many types of birds.")

 def flight(self):
 print("Most of the birds can fly but some cannot.")

 class sparrow(Bird):
 def flight(self):
 print("Sparrows can fly.")

 class ostrich(Bird):
 def flight(self):
 print("Ostriches cannot fly.")
 obj_bird = Bird()
 obj_spr = sparrow()
 obj_ost = ostrich()

 obj_bird.intro()
 obj_bird.flight() Output:-There are many types of birds.
 obj_spr.intro() Most of the birds can fly but some cannot.
 obj_spr.flight() There are many types of birds
Sparrows can fly.
 obj_ost.intro() There are many types of birds.
 obj_ost.flight() Ostriches cannot fly.
 Polymorphism with a Function and objects:
 class India():
 def capital(self):
 print("New Delhi is the capital of India.")

 def language(self):
 print("Hindi the primary language of India.")

 def type(self):
 print("India is a developing country.")
 class USA():
 def capital(self):
 print("Washington, D.C. is the capital of USA.")

 def language(self):
 print("English is the primary language of USA.")

 def type(self):
 print("USA is a developed country.")
 def func(obj):
 obj.capital()
 obj.language()
 obj.type()

 obj_ind = India()
 obj_usa = USA()

 func(obj_ind)
 func(obj_usa)
 Output:- New Delhi is the capital of India.
Hindi the primary language of India.
India is a developing country.
Washington, D.C. is the capital of USA.
English is the primary language of USA.
USA is a developed country.
 Difference between Overloading and Overriding

OVERLOADING OVERRIDING

In Method Overloading, Methods of the same class In Method Overriding, sub class have the same
shares the same name but each method must have method with same name and exactly the same number
different number of parameters or parameters having and type of parameters and same return type as a
different types and order. super class.
Method Overloading is to “add” or “extend” more to Method Overriding is to “Change” existing behavior
method’s behavior. of method.
It is a compile time polymorphism. It is a run time polymorphism.

It may or may not need inheritance in Method It always requires inheritance in Method Overriding.
Overloading.
In Method Overloading, relationship is there between In Method Overriding, relationship is there between
methods of same class. methods of super class and sub class.
 Method Overloading
 Python does not supports method overloading.
 def product(a, b):
 p=a*b
 print(p)
 def product(a, b, c):
 p = a * b*c
 print(p)
 product(4, 5) #THIS WILL GIVE ERROR
 product(4, 5, 5)
 We may use other implementation in python to make the same function work differently i.e. as
per the arguments.
 def add(datatype, *args):
 if datatype =='int':
 answer = 0
 if datatype =='str':
 answer =‘’
 for x in args:
 answer = answer + x
 print(answer)
 # Integer
 add('int', 5, 6) Output:- 11
 # String Hi Jhon
 add('str', 'Hi ‘, ‘Jhon')
 Operator Overloading in Python

 print(1 + 2) # + operator for different purposes.


 print(“Over"+“loading") # concatenate two strings
 print(3 * 4) # Product two numbers
 print(“Over"*4) # Repeat the String

 Output:- 3
GeeksFor
12
OverOverOverOver
 Overriding in Python
 # parent class

 class Parent:

 # some random function


 def anything(self):
 print('Function defined in parent class!’)

 # child class

 class Child(Parent):

Pass

 obj2 = Child()

 obj2.anything()

You might also like