Polymorphism in Java is the ability for a single method to have multiple implementations based on context, exemplified by Peter Parker's dual life as a student and Spider-Man. It is categorized into compile-time polymorphism (method overloading) and run-time polymorphism (method overriding), allowing flexibility in method behavior. The advantages of polymorphism include improved code cleanliness, ease of implementation, and enhanced reusability.
Introduction to polymorphism with examples. Polymorphism allows entities to have multiple behaviors, illustrated by Peter Parker's dual identity.
Java supports compile-time and run-time polymorphism, allowing methods to be defined with the same name for different functionalities.
Compile-time polymorphism, or method overloading, is shown through a calculator example. Different methods share the same name but handle different parameter types.
Run-time polymorphism, or method overriding, allows subclasses to redefine behavior from parent classes. Illustrated by an interest calculation example.
Key advantages of polymorphism include better code cleanliness, ease of implementation, alignment with real-world scenarios, and enhanced reusability.
Spotle.ai Study Material
Spotle.ai/Learn
3
PeterParker Is Polymorphic
As a high school
student, Peter
goes to school
and hangs out
with friends. As
Spider Man, he
bashes up
baddies. Peter
has multiple
behaviours
based on the
context. Peter is
polymorphic.
4.
Spotle.ai Study Material
Spotle.ai/Learn
Javaprovides
multiple forms of
polymorphism. It
allows you to define
the same method
name to do multiple
different things. It
also allows child
classes to redefine/
override parents’
behaviours for the
same method.
Java And Polymorphism
4
Spotle.ai Study Material
Spotle.ai/Learn
Compile-timepolymorphism refers to
behaviour that is resolved when your
Java class is compiled. Method
overloading is an example of
compile-time polymorphism. Method
overloading is how you can use
method with same name to do
different things based on the
parameters passed.
The add method in Class Calculator
for example can add 2 integers or
floats based on the types of
parameters.
Compile-Time Or Static Polymorphism
A calculator can add 2 integers. It can also add 2 floats.
The addition method adds differently based on the inputs.
6
7.
Spotle.ai Study Material
Spotle.ai/Learn
Illustration– Method Overloading
7
Public class PolymorphicCalculator
{
//Add method to add integers
int add (int a, int b)
{
System.out.println(“Adding 2 Integers”);
return a+b;
}
//Another Add method to add floats. Same name //
differentiated by data types of parameters and //
return types
float add (float a, float b)
{
System.out.println(“Adding 2 Floats”);
return a+b;
}
}
The Class
PolymorphicCalculator
provides multiple
implementation of the
method add.
8.
Spotle.ai Study Material
Spotle.ai/Learn
Illustration– Method Overloading
8
Adding a main method to
Polymorphic Calculator. The
compiler chooses the correct
add method to call based on
the data types of actual
arguments passed.
Public static void main(String args[])
{
PolymorphicCalculator calc = new
PolymorphicCalculator ();
//calling the add method with integer
arguments
System.out.println(calc.add (3,5));
//calling the add method with float arguments
System.out.println(calc.add(3.5,5.6));
}
}
The output will be:
Adding 2 Integers
8
Adding 2 Floats
9.1
9.
Spotle.ai Study Material
Spotle.ai/Learn
Run-timepolymorphism refers to
behaviour that is resolved when your
Java class is run by the JVM. Method
overriding by the sub-class is an
example of run-time polymorphism.
Method overriding allows child
classes to provide their own
implementation of a method also
defined in the parent class.
The JVM decides which version of
the method (the child’s or the
parent’s) to call based on the object
through which the method is invoked.
Run-Time Or Dynamic Polymorphism
Consider a SeniorCitizenAccount class. It
will provide a calculateInterest method()
that overrides the calculateInterest()
method in the parent SavingsAccount class
9
10.
Spotle.ai Study Material
Spotle.ai/Learn
PublicClass SavingsAccount
{
float interest;
float FixedDeposit;
//Constructor
SavingsAccount(float interest, float
FixedDeposit)
{this.seniorInterest = interest;
this.fixedDeposit = fixedDeposit;
}
//Calculate interest of parent class method
float calculateInterest()
{
System.out.println(“Calculating Savings Account
Interest.”);
return (FixedDeposit*interest/100);
}
}
Defining The Base Or Parent Class Method
10
11.
Spotle.ai Study Material
Spotle.ai/Learn
PublicClass SeniorAccount extends
SavingsAccount
{
float seniorInterest;
//constructor
SavingsAccount(float interest, float FixedDeposit)
{
this.seniorInterest = interest;
this.fixedDeposit = fixedDeposit;
}
//Overriding calculateInterest() method in parent
class
float calculateInterest()
{
System.out.println(“Calculating Senior Account
Interest.”);
return (FixedDeposit*seniorInterest/100);
}
}
Defining The Overriding Method
11
12.
Spotle.ai Study Material
Spotle.ai/Learn
Publicstatic void main (String args[])
{
SavingsAccount saving = new
SavingsAccoun(6,100000);
//This will call calculateInterest() of parent
class
System.out.println(saving.calculateInterest());
SeniorAccount senior = new
SeniorAccoun(10,100000);
//This will call calculateInterest() of parent
class
System.out.println(senior.s.calculateInterest()
);
}
Illustrating Run Time Polymorphism
12
The output is:
Calculating Savings Account Interest.
6000.0
Calculating Senior Account Interest.
10000.0