Polymorphism in Java: Spotle - Ai Study Material Spotle - Ai/learn
Polymorphism in Java: Spotle - Ai Study Material Spotle - Ai/learn
ai Study Material
Spotle.ai/Learn
Polymorphism
in Java
What Is Polymorphism?
Java provides
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.
Compile -Time
Polymorphism
Polymorphism In
Java
Run-Time
Polymorphism
//Constructor
SavingsAccount(float interest, float
FixedDeposit)
{this.seniorInterest = interest;
this.fixedDeposit = fixedDeposit;
}
//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);
}
}
Spotle.ai Study Material
11
Spotle.ai/Learn
Illustrating Run Time Polymorphism
Public static void main (String args[])
{
SavingsAccount saving = new
SavingsAccoun(6,100000);
//This will call calculateInterest() of parent
class
System.out.println(saving.calculateInterest());
Code Ease Of
Cleanliness Implementation
Reusability And
Extensibility