Inheritance defined
Inheritance is a powerful feature in object oriented
programming.
It refers to defining a new class with little or no
modification to an existing class. The new class is
called derived (or child) class and the one from
which it inherits is called the base (or parent) class.
Python Inheritance Syntax
class BaseClass:
Body of base class
class DerivedClass(BaseClass):
Body of derived class
Derived class inherits features from the base class, adding new features to it. This results
into re-usability of code.
Characteristics of Inheritance
Inheritance makes objects (classes) special
Derive new classes from existing ones
Extend the definition of existing classes
Override method definitions of existing classes
Create objects of different classes in the same
program
Allow objects to communicate with each other
Create more complex objects by combining simpler
ones
Guide to Programming with
Python
3
Inheritance Models “is a” Relationship
Guide to Programming with
Python
4
Car
Van Sports car Convertible
Animals
ReptileMammals Fish
Dog Cat Human
Using Inheritance to Create New
Classes
Inheritance: An element of OOP that allows a new
class to be based on an existing one where the
new automatically gets (or inherits) all of the
methods and attributes of the existing class
The children classes get all the capabilities
(methods) and properties (attributes) the parent
class has; the children classes are also called
derived classes
Get the code for free! (code-reuse) – inheritance
allows a new class to re-use code which already
existed in another class (the parent class)
Guide to Programming with Python 5
Derived Classes are New Classes
To create specializations of existing classes or
objects by adding new attributes and methods!
–often called subtyping when applied to classes. In
specialization, the new class or object has data or
behavior aspects that are not part of the inherited
class.
Over-ridding (e.g., over-ridding of the + operator,
so + has different meaning, addition of two
numbers, concatenation of two strings, etc) – the
same method that does something different
Guide to Programming with Python 6
Altering the Behavior of Inherited
Methods: Overriding
Override: To redefine how inherited method of
base class works in derived class
Two choices when overriding
–Completely new functionality vs. overridden method
–Incorporate functionality of overridden method, add
more
Guide to Programming with Python 7
Understanding Polymorphism
Polymorphism: Aspect of object-oriented
programming that allows you to send same
message to objects of different classes, related by
inheritance, and achieve different but appropriate
results for each object
Guide to Programming with Python 8
Working with Multiple Objects
We can have multiple objects in a program
Message: Communication between objects; one
object sends another a message when it invokes a
method of the other
(We already know that we can exchange
information among functions through parameters
and return values)
Guide to Programming with Python 9
Types of Inheritance
Guide to Programming with Python 10
Types of Inheritance
Single Inheritance
in Python
A single Python inheritance is when a single class
inherits from a class.
Python Multiple Inheritance
Multiple Python inheritance are when multiple
python classes inherit from a class.
Multilevel Inheritance in Python
When one class inherits from another, which in turn
inherits from another, it is multilevel python
inheritance.
Guide to Programming with Python 11
Types of Inheritance (Cont.)
Hierarchical Inheritance in Python
When more than one class inherits from a class, it
is hierarchical Python inheritance.
Hybrid Inheritance in Python
Hybrid Python inheritance is a combination of any
two kinds of inheritance.
Examples to try at home:
https://data-flair.training/blogs/python-inheritance/
Guide to Programming with Python 12
Types of Inheritance
Guide to Programming with Python 13
Python Inheritance Super Function – Super()
With inheritance, the super()
function in
python
actually comes in quite handy. It allows us
to call a method from the parent class. Let’s define
a new class for this.
Guide to Programming with Python 14
Python override method
A subclass may change the functionality of
a
Python method
in the superclass. It does so by
redefining it. This is termed python method
overriding. Lets see this Python Method Overriding
Example.
Guide to Programming with Python 15
Conclusion
For further practice on Object-oriented-
programming:
https://en.wikipedia.org/wiki/Inheritance_(object-
oriented_programming)
https://www.studytonight.com/python/types-of-
inheritance
https://www.programiz.com/python-programming/
inheritance
https://data-flair.training/blogs/python-inheritance/
Guide to Programming with Python 16