Object Oriented Programming: Class, Objects and Inheritance
Object Oriented Programming: Class, Objects and Inheritance
Inheritance
Prof. Rajiv Kumar
IIM Kashipur
Source:
Kamthane, A. N. & Kamthane, A.A., Programming and Problem Solving with Python, Tata McGraw-Hill
Education India. & from various sources
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
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 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.
It is a special method that is used to initialize instance variable of an object.
This method run as soon as an object of a class is instantiated.
The syntax of adding __init__ method to class is follows
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:
The area of Circle is 78.5
The __del__() (Destructor Method)
Class
Derived_Class_Name(Single_Base_Class_Name):
Body_of_Derived_Class
Inheritance continued…..
Multilevel Inheritance
When a class is derived from another derived class i.e. derived acts as base class. Such type of inheritance is
known as Multilevel Inheritance.
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.