Inheritance
Inheritance
Dr.VIDHYA B
ASSISTANT PROFESSOR & HEAD
Department of Computer Technology
Sri Ramakrishna College of Arts and Science
Coimbatore - 641 006
Tamil Nadu, India
1
Inheritance
The process of inheriting the properties of the parent
class into a child class is called inheritance.
The existing class is called a base class or parent class and the
new class is called a subclass or child class or derived class.
In object oriented programming, inheritance is an important
aspect.
The main purpose of inheritance is the reusability of code
because which can use the existing class to create a new class
instead of creating it from scratch.
In inheritance, the child class acquires all the data members,
properties, and functions from the parent class. Also, a child
class can also provide its specific implementation to the
methods of the parent class.
Inheritance
In inheritance the base class remains the same.
Inheritance uses the concept of ‘is-a’ relationship
Eg:
Teacher is-a person
Student is-a person
Pass keyword is used when it is not required to add any other properties or
methods to the class
Student class has the same properties and methods of person class
Inheriting classes in python -
Example
Inheriting classes in python -
Example
Explanation:
- Class teacher & student are both
inherited from class person
- Object of derived class is created
as derived classname followed
by empty brackets.
- The __init__() is called
automatically every time the
class is being used to create a
new object
Overriding in python
When a __init__() function is created, the child
class will no longer inherit the parents __init()
To keep the inheritance of the
parent's __init__() function, add a call to the
parent's __init__() function
Example:
Person.__init__(self, fname, lname)
Use the super() Function
Python also has a super() function that
will make the child class inherit all the
methods and properties from its parent.
Using super() function, no need to use
the name of the parent element, it will
automatically inherit the methods and
properties from its parent.
Types of Inheritance
Types of Inheritance
Types of Inheritance
Types of Inheritance
MULTIPLE INHERITANCE
MULTIPLE INHERITANCE