0% found this document useful (0 votes)
17 views12 pages

MIT11053 - Polymorphism

Uploaded by

Shibly Hasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views12 pages

MIT11053 - Polymorphism

Uploaded by

Shibly Hasan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

MIT11053- Polymorphism

MIT11053 Fundamentals of Programming 1


BY:
M.I. Fathima Nihla
Lecturer (Prob.)
Department of Management & IT
Department of MIT | SEUSL 18-Jan-24
Polymorphism
▪ In Java, polymorphism refers to a job that executes a single
operation in various ways.

▪ When there is inheritance, or when there are numerous classes


that are connected to one another, polymorphism develops.

▪ Java has a powerful inheritance capability. Java inheritance


enables a class to inherit the characteristics and features of
another class. Java's polymorphism enables us to exploit these
inherited features for a variety of purposes.
Department of MIT | SEUSL 18-Jan-24 2
Example

3
Department of MIT | SEUSL 18-Jan-24
Why Use Polymorphism?
▪ Java's polymorphism makes it easy to create methods that
can correctly handle a wide variety of functions with the
same name.

▪ We can use polymorphism to improve the consistency of our


code.

Department of MIT | SEUSL 18-Jan-24 4


Advantages of Polymorphism
▪ It allows the code to be reused. Classes that have been created, tested, and
implemented can be used repeatedly. Additionally, it saves the coder the
time. Additionally, the code can be modified without changing the original
code.

▪ Multiple data values can be kept in a single variable. You can change the
value of a variable in a subclass that you inherit from without altering the
value of that variable in the superclass or any other subclasses.

▪ It is simpler for the programmer to debug the code when there are fewer
lines of code.
Department of MIT | SEUSL 18-Jan-24 5
How to Achieve Polymorphism
in Java
▪ Method Overloading

▪ Method Overriding

Department of MIT | SEUSL 18-Jan-24 6


Polymorphism Using Method
Overriding
▪ If the same method is present in both the superclass and
the subclass during inheritance in Java. Then, the method in
the superclass is then replaced by the subclass's version. It's
known as method overriding.

▪ In this scenario, one operation will be carried out in the


superclass and another in the subclass by the same method.

Department of MIT | SEUSL 18-Jan-24 7


Example

8
Department of MIT | SEUSL 18-Jan-24
Polymorphism Using Method
Overloading
Recall what we have studied under
‘METHOD OVERLOADING’!!!

Department of MIT | SEUSL 18-Jan-24 9


Example

10
Department of MIT | SEUSL 18-Jan-24
Difference Between
Overloading and Overriding
Method Overloading Method Overriding

• When multiple methods with the same • When a method from the super class is
name but varying implementations are redefined in the sub-class, this is
present in the same class, this is known as method overriding. The
known as method overloading. method's signature stays the same in
this situation.

Department of MIT | SEUSL 18-Jan-24 11


12
END OF LECTURE

Thank You!!

Department of MIT | SEUSL 18-Jan-24

You might also like