PWP Unit 5
PWP Unit 5
o Classes :
▪ Python is an object oriented programming language.
▪ Almost everything in Python is an object, with its properties and
methods.
▪ Class is a collection of attributes and methods.
▪ A Class is like an object constructor, or a "blueprint" for creating
objects.
▪ Syntax:
Class Class_name : #here we created empty class.
Pass
▪ Example :
Class Rectangle :
o Objects :
▪ Object is an instance of class.
▪ Objects are used to access the attributes and methods of the class.
▪ Syntax:
Object name=class name ( )
▪ Example :
Rect = Rectangle()
o init method :
▪ Python supports very unique method named as init () method.
▪ This method gets executed when instance of a class gets created.
▪ It is also called as initialize method as constructor in oop.
▪ Use the init () function to assign values to object properties, or
other operations that are necessary to do when the object is being
created
▪ Example :
Class Rectangle:
def init (self):
self.lengt=10
self.width=12
def rectangle_area(self):
return self.length*self.width
Rect=Rectangle()
Print(“thearea of rectangle is :”,Rect.rectangle_area)
❖ Method Overloading :
o Method overloading is the ability to define the method with the same
name but with a different number of arguments and data types.
o With this ability one method can perform different tasks, depending on
the number of arguments or the types of the arguments given.
o Method overloading is a concept in which a method in a class performs
operations according to the parameters passed to it.
Output:
Addition of integer numbers= 30
Addition of integer numbers= 23.25
Addition of integer numbers= HelloPython
o Python does not support method overloading, that is, it is not possible to
define more than one method with the same name in a class in Python.
o This is because method arguments in python do not have a type. A
method accepting one argument can be called with an integer value, a
string or a double as shown in next example.
class Demo:
def method(self, a):
print(a)
obj= Demo()
obj.method(50)
obj.method('Meenakshi')
obj.method(100.2)
Output:
50
Meenakshi
100.2
❖ Method Overriding :
o Method overloading is the ability to define the method with the same
name and same number of arguments and data types.
o Method overriding is an ability of a class to change the implementation of
a method provided by one of its base class.
o Method overriding is thus a strict part of inheritance mechanism.
o To override a method in base class, we must define a new method with
sam name and same parameters in the derived class.
o Overriding is a very important part of OOP since it is feature that makes
inheritance exploit its full power.
o Through method overriding a class may “copy” another class, avoiding
duplicated code and at the same time enhance or customize part of it.
class A:
def display(self):
print("This is base class")
class B(A):
def display(self):
print("This is derived class")
obj=B() # instance of child
obj.display() # child class overriden method
Output :
This is derived class
o The super() method gives you access to methods in a super class from
the subclass that inherits from it.
oThe super() method returns a temporary object of the superclass that then
allows you to call that superclass’s method.
Output :
This is base class
This is derived class
❖ Data Hiding :
Example :
class student:
def init (self):
self.rollno=1 # public variable
self. name="ABC" # private variable
S1=student()
print(S1.rollno)
print(S1. name)
o after execution of above code you will get a error , ‘student’ object
has no attribute “ name”. So we cannot access private members
outside the class, we have to used those members inside the class.
Class student:
def init (self):
self.rollno=1 # public variable
self. name="AB # private variable
def display(self):
print(self.rollno)
print(self. name)
S1=student()
S1.display()
❖ Data Abstraction :
o We can restrict access of methods and variables in a class with the help
of encapsulation. It will prevent the data being modified by accident.
o Encapsulation is used to hide the value or state of a structured data
object inside a class, preventing unauthorized parties direct access to
them.
o Data abstraction refers to providing only essential information about the
data to the outside world, hiding the background details of
implementation.
o Encapsulation is a process to bind data and functions together into a
single unit i.e. class while abstraction is a process in which the data
inside the class is the hidden from outside world.
o In short hiding internal details and showing functionality is known as
abstraction.
o To support encapsulation, declare the methods or variables as private in
the class. The private methods cannot be called by the object directly. It
can be called only from within the class in which they are defined.
o Any function with double underscore is called private method.
Access modifiers for variables and methods are:
oPublic methods / variables- Accessible from anywhere inside the class,
in the sub class, in same script file as well as outside the script file.
oPrivate methods / variables- Accessible only in their own class. Starts
with two underscores.
Example: For access modifiers with data abstraction
class student:
a=10 #private variable
b=20 #public variable
def private_method(self): #private method
print("Private method is called")
def public_method(self): #public method
print("public method is called")
print("a= ",self. a) #can be accessible in same class
s1=student()
# print("a= ",s1. a) #generate error
print("b=",s1.b)
# s1. private_method() #generate error
s1.public_method()
Output:
b= 20
public method is called
a= 10
❖ Inheritance :
class Parent1:
def func_1(self):
print ("This function is defined inside the parent class.")
# Driver's code
object = Child1()
object.func_1()
object.func_2()
Output :
This function is defined inside the parent class.
This function is defined inside the child class.
Example 2:
class vehicle: #parent class name="Maruti"
def display(self):
print("Name= ",self.name)
class category(vehicle): # drived class
price=400000
def disp_price(self):
print("price= ",self.price)
car1=category()
car1.display()
car1.disp_price()
Output:
Name= Maruti
price= 400000
❖ Multilevel Inheritance :
In multilevel inheritance, features of the base class and the derived class are further
inherited into the new derived class. This is similar to a relationship representing a
child and grandfather.
Example :
class Mother1:
mothername1 = " "
def mother1(self):
print(self.mothername1)
# Driver's code
s1 = Son1()
s1.fathername1 = "Rajesh"
s1.mothername1 = "Shreya"
s1.parents1()
Output :
❖ Multilevel inheritance :
o Multilevel inheritance, the features that are part of the original class, as well
as the class that is derived from it, are passed on to the new class. It is
similar to a relationship involving grandparents and children.
def print_name(self):
print('Grandfather name is :', self.grandfathername1)
print("Father name is :", self.fathername1)
print("Son name is :", self.sonname1)
# Driver code
s1 = Son1('John', 'John Jr', 'John Jr Jr')
print (s1.grandfathername1)
s1.print_name()
Output :
John Jr Jr
Grandfather name is : John Jr Jr
Father name is : John Jr
Son name is : John
❖ Hierarchical Inheritance :
# Derived class1
class Child_1(Parent1):
def func_2(self):
print ("This function is defined inside the child 1.")
# Derivied class2
class Child_2(Parent1):
def func_3(self):
print ("This function is defined inside the child 2.")
# Driver's code
object1 = Child_1()
object2 = Child_2()
object1.func_1()
object1.func_2()
object2.func_1()
object2.func_3()
Output :
This function is defined inside the parent class.
This function is defined inside the child 1.
This function is defined inside the parent class.
This function is defined inside the child 2.
❖ Composition classes :
Class GenericClass:
Define some attributes and methods
Class AspecificClass:
Instance_variable_of_generic_class=GenericClass
#use this instance somewhere in the class
Some_method(instance_varable_of_generic_class)
o For example, we have three classes email, gmail and yahoo. In email
class we are referring the gmail and using the concept of composition.
class gmail:
def send_email(self,msg):
print("sending '{}' from gmail".format(msg))
class yahoo:
def send_email(self,msg):
print("sending '{}' from yahoo".format(msg))
class email:
provider=gmail()
def set_provider(self,provider):
self.provider=provider
def send_email(self,msg):
self.provider.send_email(msg)
client1=email()
client1.send_email("Hello")
client1.set_provider(yahoo())
client1.send_email("Hello")
Output:
sending 'Hello' from gmail
sending 'Hello' from yahoo
Output :
This is derived class
This is base class