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

Object Oriented Programming: Class, Objects and Inheritance

This document discusses object oriented programming concepts in Python including classes, objects, attributes, methods, inheritance, and the special methods __init__ and __del__. It provides an example class to calculate the area of a circle and uses inheritance to create a derived class that inherits from an existing base class.

Uploaded by

Rajat Singh
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)
46 views

Object Oriented Programming: Class, Objects and Inheritance

This document discusses object oriented programming concepts in Python including classes, objects, attributes, methods, inheritance, and the special methods __init__ and __del__. It provides an example class to calculate the area of a circle and uses inheritance to create a derived class that inherits from an existing base class.

Uploaded by

Rajat Singh
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/ 18

Object Oriented Programming : Class, Objects and

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

 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.

 The Syntax for defining Class is as follows


class Class_Name:
Initializer
attributes
methods()
statements
A simple class program
 Write a simple class program and print the message “Welcome to 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

The following syntax is used to access attributes of a class


<object>.<attribute>

 Assigning values to an attributes outside the class


<object>.attribute = Value
Example:
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.
 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)

 The method __del__ denotes the destructor


Following is the syntax to define destructor.
Syntax:
def __del__(self):
block

 Python invokes destructor method when instance is about to be destroyed.


 The self refers to the instance on which __del__() method invoked.
Inheritance
 Inheritance is one of the most useful and essential characteristics of object-oriented programming.
 The existing classes are main components of inheritance. The new classes are created from existing one.
 The properties of existing classes are simply extended to the new classes.
 The new classes created using such method are known as derived classes or sub class and the existing
classes are known as base class or super class.
Example of Inheritance
Type of Inheritance
 Single Inheritance
Only one base class is used for derivation of a new class. Further, the derived class is not used as base class.

Syntax to inherit single Base class:

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.

You might also like