Inheritance in Python
S.N.B.P College of Art’s Commerce Science & Management
Studies Pimpri, Pune-18
SAVITRIBAI PHULE PUNE UNIVERSITY
2021-2022
PROJECT REPORT
ON
“INHERITANCE IN PYTHON”
SUBMITTED IN PARTIAL FULFILLMENT OF THE REQUIREMENT
FOR THE AWARD OF DEGREE OF
BACHELOR OF BUSINESS ADMINISTRATION COMPUTER
APPLICATION
SUBMITTED BY:-
PRASHANT SUDHAKAR LAMB
&
GURUDATTA KAMBLE
UNDER GUIDANCE OF
Prof. Jayashree Gholap
1
Inheritance in Python
CERTIFICATE
This is to certify that Project Report entitled “INHERITANCE IN PYTHON”
with reference to SNBP college of Arts, commerce, science and Management
studies, Morwadi, Pimpri, Pune-18 which is being submitted here with the
completion of Bachelor of Business Administration (Computer Application) of
Semester V of Savitribai Phule Pune University is the result of the original work
completed by Mr. PRASHANT SUDHAKAR LAMB (BC213018) and Mr.
GURUDATTA KAMBLE (BC213014) under my supervision and guidance and
to the best of my knowledge and belief, the work complied in this project report
is original & not formed earlier.
Project Guide Lab In-charge Program Co-coordinator
Academic Head Seal Principal
Verified By
Seat No. Date:
Internal Examiner External Examiner
(Sign & Name) (Sign & Name)
2
Inheritance in Python
ACKNOWLEDGEMENT
“When we start a journey towards something worthwhile which is
never a simple trail or an easy mile, but we often move on without
looking back. At all the people who helped put us on track, so today
we have reached the end of our journey, we’d like to thank of all
those who walked with us”
“ALONE WE CAN DO LITTLE”
“BUT TOGETHER WE CAN DO SO MUCH”
Before going into intricacies of the project, we would like to thank
our Academic Head “Prof. Umeshwari Patil” and all staff
members of S.N.B.P college of A.C.S pimpri pune-18.
We express our special thanks to “Prof. Jayashree Gholap”
(project guide), and ALL TYBBA(CA) STAFF for their inspiring
guidance and constant encouragement throughout endeavor, which
enable us to successfully complete this project work, we are grateful
for their invaluable assistance and support at all stage.
Sincerely,
-Prashant Sudhakar Lamb
-Gurudatta Kamble
T.Y.BBA (CA)
3
Inheritance in Python
Declaration
It is hereby declared the all the facts and figures included
in the project is results of my own research and investigation
including formal analysis of the entire research work and the
same has not been previously submitted to any examination of
the University or any other University.
This declaration will hold a good and in my wise behalf
with the full consciousness.
Date:
Place: Morwadi
Name & Signature of the student
4
Inheritance in Python
CONTENTS
SR NO. Chapter name and Page No.
contents
01 Class and objects 6
in python
02 Classes 7
03 Inheritance 8
04 Object class 10
05 Types of 12
inheritance
5
Inheritance in Python
Chapter 1
CLASS And OBJECTS IN PYTHON
A class is a user-defined blueprint or prototype from which objects
are created. Classes provide a means of bundling data and
functionality together. Creating a new class creates a new type of
object, allowing new instances of that type to be made. Each class
instance can have attributes attached to it for maintaining its state.
Class instances can also have methods (defined by their class) for
modifying their state.
To understand the need for creating a class let’s consider an example,
let’s say you wanted to track the number of dogs that may have
different attributes like breed, age. If a list is used, the first element
could be the dog’s breed while the second element could represent its
age. Let’s suppose there are 100 different dogs, then how would you
know which element is supposed to be which? What if you wanted to
add other properties to these dogs? This lacks organization and it’s the
exact need for classes.
Class creates a user-defined data structure, which holds its own data
members and member functions, which can be accessed and used by
creating an instance of that class. A class is like a blueprint for an
object.
6
Inheritance in Python
CHAPTER 2
CLASSES
Classes provide a means of bundling data and functionality together.
Creating a new class creates a new type of object, allowing new
instances of that type to be made. Each class instance can have
attributes attached to it for maintaining its state. Class instances can
also have methods (defined by its class) for modifying its state.
Compared with other programming languages, Python’s class
mechanism adds classes with a minimum of new syntax and semantics.
It is a mixture of the class mechanisms found in C++ and Modula-3.
Python classes provide all the standard features of Object Oriented
Programming: the class inheritance mechanism allows multiple base
classes, a derived class can override any methods of its base class or
classes, and a method can call the method of a base class with the same
name. Objects can contain arbitrary amounts and kinds of data. As is
true for modules, classes partake of the dynamic nature of Python:
they are created at runtime, and can be modified further after
creation.
In C++ terminology, normally class members (including the data
members) are public (except see below Private Variables), and all
member functions are virtual. As in Modula-3, there are no
shorthands for referencing the object’s members from its methods:
the method function is declared with an explicit first argument
representing the object, which is provided implicitly by the call. As in
Smalltalk, classes themselves are objects. This provides semantics for
importing and renaming. Unlike C++ and Modula-3, built-in types
can be used as base classes for extension by the user. Also, like in C++,
most built-in operators with special syntax (arithmetic operators,
subscripting etc.) can be redefined for class instances.
(Lacking universally accepted terminology to talk about classes, I will
make occasional use of Smalltalk and C++ terms. I would use Modula-
3 terms, since its object-oriented semantics are closer to those of
Python than C++, but I expect that few readers have heard of it.)
7
Inheritance in Python
CHAPTER 3
INHERITANCE
Inheritance is the capability of one class to derive or inherit the
properties from another class. The benefits of inheritance are:
1. It represents real-world relationships well.
2. It provides reusability of a code. We don’t have to write the same
code again and again. Also, it allows us to add more features to a
class without modifying it.
3. It is transitive in nature, which means that if class B inherits from
another class A, then all the subclasses of B would automatically
inherit from class A.
8
Inheritance in Python
EXAMPLE
# A Python program to demonstrate inheritance
# Base or Super class. Note object in bracket.
# (Generally, object is made ancestor of all classes)
# In Python 3.x "class Person" is
# equivalent to "class Person(object)"
class Person(object):
# Constructor
def __init__(self, name):
self.name = name
# To get name
def getName(self):
return self.name
# To check if this person is an employee
def isEmployee(self):
return False
# Inherited or Subclass (Note Person in bracket)
class Employee(Person):
# Here we return true
def isEmployee(self):
return True
# Driver code
emp = Person("Geek1") # An Object of Person
print(emp.getName(), emp.isEmployee())
emp = Employee("Geek2") # An Object of Employee
print(emp.getName(), emp.isEmployee())
9
Inheritance in Python
CHAPTER 4
OBJECT CLASS
Like Java Object class, in Python (from version 3.x), object is root of
all classes.
In Python 3.x, “class Test(object)” and “class Test” are same.
In Python 2.x, “class Test(object)” creates a class with object as
parent (called new style class) and “class Test” creates old style class
(without object parent).
Subclassing (Calling constructor of parent class)
A child class needs to identify which class is its parent class. This
can be done by mentioning the parent class name in the definition of
the child class.
10
Inheritance in Python
Eg: class subclass_name (superclass_name):
# Python code to demonstrate how parent constructors are called
parent class.
class Person( object ):
# __init__ is known as the constructor
def __init__(self, name, idnumber):
self.name = name
self.idnumber = idnumber
def display(self):
print(self.name)
print(self.idnumber)
# child class
class Employee( Person ):
def __init__(self, name, idnumber, salary, post):
self.salary = salary
self.post = post
# invoking the __init__ of the parent class
Person.__init__(self, name, idnumber)
# creation of an object variable or an instance
a = Employee('Rahul', 886012, 200000, "Intern")
# calling a function of the class Person using its instance
11
Inheritance in Python
CHAPTER 5
TYPES OF INHERRITANCE IN PYTHON
Types of Inheritance depend upon the number of child and parent
classes involved. There are four types of inheritance in Python:
1. Single Inheritance: Single inheritance enables a derived class to
inherit properties from a single parent class, thus enabling code
reusability and the addition of new features to existing code.
12
Inheritance in Python
2. Multiple Inheritances: When a class can be derived from more
than one base class this type of inheritance is called multiple
inheritances. In multiple inheritances, all the features of the base
classes are inherited into the derived class.
13
Inheritance in Python
3. 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.
14
Inheritance in Python
4. Hierarchical Inheritance: When more than one derived
classes are created from a single base this type of inheritance is
called hierarchical inheritance. In this program, we have a
parent (base) class and two child (derived) classes.
15