Chapter 10
Chapter 10
Chapter 10
CHAPTER 10
OBJECT ORIENTED
PROGRAMMING :
CLASS, OBJECTS AND
INHERITANCE
PROPRIETARY MATERIAL © 2018 The McGraw Hill Education, Inc. All rights reserved. No part of this PowerPoint slide may be displayed, reproduced
or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers
and educators permitted by McGraw Hill for their individual course preparation. If you are a student using this PowerPoint slide, you are using it
without permission.
Introduction
Python is an object oriented language.
Object oriented languages helps programmer to
reduce complexity of programs
The general concept of object oriented programming
language is about class.
The class is just another name for a type in
python.
Where as Objects are instances of a class
An object is a container of data: attributes
An object has associated functions: methods
Defining Classes
The class is just another name for type in python.
The class may contain data in the form of fields.
Fields are also called as attributes and code in the
form of procedure known as methods.
class MyFirstClassProgram:
print(' Welcome to Classes')
C=MyFirstClassProgram() #Instance of class.
print(C)
Output:
Welcome to Classes
<__main__.MyFirstClassProgram object at
0x00000000067BF908>
Adding, Assigning and Accessing values to an
Attributes
Adding attribute to the class
The syntax to add attribute to the class is as follows
class class_name:
attribute_name = value
…………………………………………………………
…………………………………………………………
Accessing attributes of a class
class Rectangle:
length=0; #Attribute length
breadth=0; #Attribute breadth
R1 = Rectangle () #Instance of a class
print('Initial values of Attribute')
print('Length = ',R1.length) #Access attribute length
print('Breadth = ',R1.breadth) #Access attribute breadth
print('Area of Rectangle = ',R1.length * R1.breadth )
R1.length = 20 #Assign value to attribute length
R1.breadth = 30 #Assign value to attribute breadth
print('After reassigning the value of attributes')
print('Length = ',R1.length )
print('Breadth = ',R1.breadth )
print('Area of Rectangle is ',R1.length * R1.breadth)
Output:
Initial values of Attribute
Length = 0
Breadth = 0
Area of Rectangle = 0
After reassigning the value of attributes
Length = 20
Breadth = 30
Area of Rectangle is 600
Adding Methods to The Class
Class consist of two things i.e. instance variable
and instance methods.
The syntax to add methods to the class is follows
class Class_Name:
instance variable; #instance variable with initialization
def mthod_name(Self,parameter_list):#Paramter List is Optional
block_of_statements
Note:
The first parameter for each method should be self if
method exist within the class.
The self parameter references the object itself.
Program:
Program to create method Display_Message() display
message “Hello, Learn Adding Methods” within the
methods.
class Demo:
def Display_Message(self):
print('Hello, Learn Adding Methods')
D1 = Demo()
D1.Display_Message()1
Output:
'Hello, Learn Adding Methods
The __init__ method (Constructor)
The __init__ method is known as an initializer.
class Class_Name:
def _init_(self): #__init__ method
…………………………
…………………………
Note:
a) __init__ method must have self as first argument.
b) As self refers to the object itself. Therefore it refers to the object that
invokes the method.
Program
Write a program to calculate area of circle by making use
of __init__ method.
class Circle:
def __init__(self,pi):
self.pi = pi
def calc_area(self,radius):
return self.pi*radius**2
C1=Circle(3.14)
print(' The area of Circle is ',C1.calc_area(5))
Output:
Syntax:
Class Derived_Class_Name(Comma_Seperated_Base_Class_Names):
Body_of_Derived_Class
Inheritance continued…..
Multiple Inheritance
When two or more base classes are used for
derivation of a new class is called multiple
Inheritance.
Program: A simple example of
Inheritance
class A:
def Message1(self):
print('I am in A')
class B(A):
def Message2(self):
print('I am in B')
D1 = B()
D1.Message2()
D1.Message1()
Output:
I am in B
I am in A
Conclusion……
class and objects that reduces the complexity of
programs.
Attributes and member function belongs to the class.
The self parameter is used to reference object itself
A programmer can initialize the value of member variable
or attribute by making use of __init__ method.
Python also supports various types of inheritance such
as single inheritance, multiple and multi level
inheritance.