MIT11053 - Polymorphism
MIT11053 - Polymorphism
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.
▪ 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
8
Department of MIT | SEUSL 18-Jan-24
Polymorphism Using Method
Overloading
Recall what we have studied under
‘METHOD OVERLOADING’!!!
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.
Thank You!!