CMPE 103
Module 7
METHOD OVERLOADING AND
OVERRIDING
At the end of the module, you should be able to:
● Recall the concept of Polymorphism
● discuss the two types of Polymorphism
● discuss the concept of Method Overloading
● demonstrate application programs on Method
Overloading
● discuss the concept of Method Overriding
Polymorphism
The word “POLYMORPHISM” comes from two Greek words
POLY and MORPHISM” which means many forms. It is
important feature in Python that allows us to define
methods in the child class with the same name as defined in
their parent class.
There are two types of Polymorphism:
(1) Overloading
(2) Overriding
Feature Method Overriding Method Overloading
Defining multiple methods in the same scope
Subclass provides a specific
(usually in the same class) with the same name
Definition implementation of a method already
but different signatures (number or type of
defined in its superclass.
parameters).
To change or extend the behavior of To allow different ways to call a method with
Purpose
an inherited method. different parameters.
Must have the same name and
Must have the same method name but different
Method Signature parameters as the method in the
parameters (not directly supported in Python).
superclass.
Does not require inheritance. Usually within the
Requires a class hierarchy
Inheritance same class.
(inheritance).
is used when you need to change the allows you to create multiple methods with the
Summary behavior of inherited methods in a same name but different parameters.
subclass. (not directly supported in Python).
Method Overloading
- Occurs when two or more methods in
one class have the same method name
but different parameters.
- allows you to create multiple methods
with the same name but different
parameters .
Method Overriding
- Means having two methods with the same
method name and parameters (i.e., METHOD
SIGNATURE). One of the methods is in parent
class and the other is in the child class.
What do you think the output of
this program?
Operator Overloading and Overriding