2. Object oriented Concepts: Creating class, Creating object
• Class- Classes are defined by the user. The class provides the
basic structure for an object. It consists of data members and
method members that are used by the instances(object) of the
class.
• Object- A unique instance of a data structure that is defined by
its class. An object comprises both data members and methods.
Class itself does nothing but real functionality is achieved through their
objects.
3. Creating Classes:
• Syntax :-
class ClassName:
#list of python class variables
# Python class constructor
#Python class method definitions
• In a class we can define variables, functions etc. While writing function in class we have to
pass atleast one argument that is called self parameter.
• The self parameter is a reference to the class itself and is used to access variables that
belongs to the class.
4. Example:
Example: Creating class
class student:
def display(self):
print("Hello Python")
In python programming self is a default variable that contains the memory address
of the instance of the current class.
Creating Objects-Objects can be used to access the attributes of the class
s1=student() #creating object of class
s1.display() #calling method of class using object
5. Instance variable and Class variable:
• Instance variable is defined in a method and its scope is only within the object
that defines it
• Class variable is defined in the class and can be used by all the instances of that
class.
• Instance variables are unique for each instance, while class variables are shared
by all instances.
6. Example: For instance and class variables
• class sample:
x=2 # x is class variable
def get(self,y): # y is instance variable
self.y=y
s1=sample()
s1.get(3)
print(s1.x," ",s1.y)
s2=sample()
S2.get(4)
print(s2.x," ",s2.y)
7. Example: Class with get and put method
• class car:
def get(self,color,style):
self.color=color
self.style=style
def put(self):
print(self.color)
print(self.style)
• c=car()
• c.get('Black','Red')
• c.put()
8. Method Overloading
• Method overloading is the ability to define the method with the same
name but with a different number of arguments and data types.
• Python does not support method overloading i.e it is not possible to
define more than one method with the same name in a class in python.
• This is because method arguments in python do not have a type.
9. Method Overloading
# To calculate area of rectangle
• def area(length,breadth):
calc=length*breadth
print(calc)
• # To calculate area of square
• def area(size):
calc=size*size
print(calc)
area(3)
area(4,5)
10. • Output9
Traceback (most recent call last):
File "D:python programstrial.py", line 10, in <module>
area(4,5)
TypeError: area() takes 1 positional argument but 2 were given
11. Data hiding
• Data hiding is a software development technique specifically used in object
oriented programming to hide internal object details(data members).
• Data hiding is also known as information hiding. An objects attributes may
or may not be visible outside the class definition
• In Python, you can achieve data hiding by using a double underscore prefix
(__) before an attribute or method name
• Any variable prefix
• with double underscore is called private variable which is accessible only
• with class where it is declared
12. • class counter:
• __secretcount=0 # private variable
• def count(self): # public method
• self.__secretcount+=1
• print("count= ",self.__secretcount) # accessible in the same class
• c1=counter()
• c1.count() # invoke method
• c1.count()
• print("Total count= ",c1.__secretcount) # cannot access private variable directly
14. Data abstraction
• Data abstraction refers to providing only essential information about the data to
the outside world, hiding the background details of implementation.
• In short hiding internal details and showing functionality is known as abstraction.
• Access modifiers for variables and methods are:
• Public methods / variables- Accessible from anywhere inside the class, in the sub
class, in same script file as well as outside the script file.
• Private methods / variables- Accessible only in their own class. Starts with two
underscores.
15. 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
Output: b= 20
public method is called
a= 10
16. Encapsulation
• 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
17. Creating Constructor:
• Constructors are generally used for instantiating an object.
• The task of constructors is to initialize(assign values) to the data
members of the class when an object of class is created.
• In Python the __init__() method is called the constructor and is
always called when an object is created.
• Syntax of constructor declaration :
• def __init__(self):
# body of the constructor
18. Example: For creating constructor use_ _init_ _ method called
as constructor.
class student:
def __init__(self,rollno,name,age):
self.rollno=rollno
self.name=name
self.age=age
print("student object is created")
p1=student(11,"Ajay",20)
print("Roll No of student= ",p1.rollno)
print("Name No of student= ",p1.name)
print("Age No of student= ",p1.age)
Output:
student object is created
Roll No of student= 11
Name No of student= Ajay
Age No of student= 20
19. • Define a class rectangle using length and width.It has a method which
can compute area.
• Create a circle class and initialize it with radius. Make two methods
getarea and getcircumference inside this class
20. Types of Constructor:
• There are two types of constructor-
• Default constructor
• Parameterized constructor
21. Default constructor-
• The default constructor is simple constructor which does not accept any
arguments. Its definition has only one argument which is a reference to the
instance being constructed.
class student:
def __init__(self):
print("This is non parameterized constructor")
def show(self,name):
print("Hello",name)
s1=student()
s1.show("World")
22. Parameterized constructor-
• Constructor with parameters is known as parameterized constructor.
• The parameterized constructor take its first argument as a reference to the
instance being constructed known as self and the rest of the arguments are
provided by the programmer.
23. Example: For parameterized constructor
class student:
def __init__(self,name):
print("This is parameterized constructor")
self.name=name
def show(self):
print("Hello",self.name)
s1=student("World“)
s1.show()
24. Destructor:
• A class can define a special method called destructor with the help of _ _del_
_().
• It is invoked automatically when the instance (object) is about to be destroyed.
• It is mostly used to clean up non memory resources used by an instance(object).
Example: For Destructor
class student:
def __init__(self):
print("This is non parameterized constructor")
25. Example: For Destructor
class student:
def __init__(self):
print("This is non parameterized constructor")
def __del__(self):
print("Destructor called")
• s1=student()
• s2=student()
• del s1
26. Inheritance:
• The mechanism of designing and constructing classes from other classes is called
inheritance.
• Inheritance is the capability of one class to derive or inherit the properties from
some another class.
• Single Inheritance
• Multiple Inheritance
• Multilevel Inheritance
• Hierarchical Inheritance
27. Single Inheritance
• Single inheritance enables a derived class to inherit properties from a single
parent class
• Syntax:
Class A:
# Properties of class A
Class B(A):
# Class B inheriting property of class A
# more properties of class B
28. Example 1: Example of Inheritance without using constructor
class vehicle:
name="Maruti"
def display(self):
print("Name= ",self.name)
class category(vehicle):
price=400000
def disp_price(self):
print("price= ",self.price)
car1=category()
car1.display()
car1.disp_price()
29. Example 2: Example of Inheritance using constructor
class vehicle:
def __init__(self,name,price):
self.name=name
self.price=price
def display(self):
print("Name= ",self.name)
class category(vehicle):
def __init__(self,name,price):
vehicle.__init__(self,name,price) #pass data to base constructor
def disp_price(self):
print("price= ",self.price)
car1=category("Maruti",400000)
car1.display()
car1.disp_price()
30. 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
• Syntax:
Class A:
# Properties of class A
Class B(A):
# Class B inheriting property of class A
# more properties of class B
Class C(B):
# Class C inheriting property of class B
# thus, Class C also inherits properties of class A
32. Multiple Inheritance:
• When a class can be derived from more than one base classes this type of
inheritance is called multiple inheritance. In multiple inheritance, all the features of
the base classes are inherited into the derived class.
Syntax:
Class A:
# variable of class A
# functions of class A
Class B:
# variable of class B
# functions of class B
Class C(A,B):
# Class C inheriting property of both class A and B
33. Example: Python program to demonstrate multiple inheritance
class Father:
def display1(self):
print("Father")
class Mother:
def display2(self):
print("Mother")
class Son(Father,Mother):
def display3(self):
print("Son")
s1 = Son()
s1.display3()
s1.display2()
s1.display1()
34. Hierarchical Inheritance:
• When more than one derived classes are created from a single base this type of
inheritence is called hierarchical inheritance. In this program, we have a parent
(base) class and two child (derived) classes.
35. Example : Python program to demonstrate Hierarchical inheritance
class Parent:
def func1(self):
print("This function is in parent class.")
class Child1(Parent):
def func2(self):
print("This function is in child 1.")
class Child2(Parent):
def func3(self):
print("This function is in child 2.")
object1 = Child1()
object1.func1()
object1.func2()