CMPE-103-Module-5-Inheritance-and-Polymorphism
CMPE-103-Module-5-Inheritance-and-Polymorphism
Module 5
INHERITANCE & POLYMORPHISM
At the end of the module, you should be able to:
● define Inheritance and Polymorphism
● discuss the syntax and program structure of Inheritance
● demonstrate application programs on inheritance
● discuss the types of inheritance
● demonstrate application program on the different type of inheritance
● Intro to polymorphism concept
● discuss the concept of overloading and Overriding
● demonstrate application programs on overloading and overriding
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.
● The main purpose of inheritance is the reusability of code because we 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
For example, In the real world, Car is a sub-class of a Vehicle class. We can create a Car
by inheriting the properties of a Vehicle such as Wheels, Colors, Fuel tank, engine, and
add extra properties in Car as required.
Inheritance – “IS A” or “IS A KIND OF” Relationship
Example – “IS A” Relationship
How to implement Inheritance in Python
Create a Parent Class
class Person:
def __init__(self, fname, lname): Create a Child Class
self.firstname = fname
self.lastname = lname class Student(Person):
pass
def printname(self):
print(self.firstname, self.lastname) ‘’’Use the Student class to create an object,
and then execute the printname method’’’
x = Person(“Jose", “Rizal")
x.printname()
Types Of Inheritance
In Python, based upon the number of child and parent classes involved, there are five
types of inheritance. The type of inheritance are listed below:
➢ Single inheritance
➢ Multiple Inheritance
➢ Multilevel inheritance
➢ Hierarchical Inheritance
➢ Hybrid Inheritance
Single Inheritance
In single inheritance, a child class inherits from a single-parent class. Here is one
child class and one parent class.
Output:
Multiple Inheritance
In multiple inheritance, one child class can inherit from multiple parent classes. So here is one child
class and multiple parent classes.
Output:
Multilevel inheritance
In multilevel inheritance, a class inherits from a child class or derived class. Suppose three classes A, B, C. A is the
superclass, B is the child class of A, C is the child class of B. In other words, we can say a chain of
classes is called multilevel inheritance.
Output:
Hierarchical Inheritance
In Hierarchical inheritance, more than one child class is derived from a single parent class.
In other words, we can say one parent class and multiple child classes.
Output:
Hybrid Inheritance
When inheritance is consists of multiple types or a combination of different inheritance is called hybrid
inheritance.
Output:
Python super() function
When a class inherits all properties and behavior from the parent class is called inheritance. In such a case, the inherited class
is a sub class and the latter class is the parent class. In child class, we can refer to parent class by using the
super()function.
When a child class method has the same name, same parameters, and same return type as a method in its
superclass, then the method in the child is said to override the method in the parent class.
Output:
In the above example, we create two classes named Vehicle (Parent Class) and Car (Child Class) The class Car
extends from the class Vehicle so, all properties of the parent class are available in the child class. In addition to
that, the child class redefined the method max_speed().
What is Polymorphism in Python?
Polymorphism in Python is the ability of an object to take many forms. In simple words, polymorphism
allows us to perform the same action in many different ways. For example, Jessa acts as an employee
when she is at the office. However, when she is at home, she acts like a wife. Also, she represents herself
differently in different places. Therefore, the same person takes different forms as per the situation.
Polymorphism in Built-in function len()
The built-in function len() calculates the length of an object depending upon its type. If an object is a string, it returns
the count of characters, and If an object is a list, it returns the count of items in a list.
Output:
Polymorphism With Inheritance
Polymorphism is mainly used with inheritance. In inheritance, child class inherits the attributes and methods of a parent
class. 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.
Using method overriding polymorphism allows us to defines methods in the child class that have the same name as the
methods in the parent class. This process of re-implementing the inherited method in the child class is known as
Method Overriding.