0% found this document useful (0 votes)
37 views

Polymorphism in Java: Spotle - Ai Study Material Spotle - Ai/learn

The document discusses polymorphism in Java. It defines polymorphism as one entity providing multiple implementations or behaviors. It explains that Java supports two types of polymorphism: compile-time polymorphism through method overloading, and run-time polymorphism through method overriding in subclasses. It provides examples of how method overloading and overriding work in Java and the advantages of polymorphism.

Uploaded by

md anik hasan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Polymorphism in Java: Spotle - Ai Study Material Spotle - Ai/learn

The document discusses polymorphism in Java. It defines polymorphism as one entity providing multiple implementations or behaviors. It explains that Java supports two types of polymorphism: compile-time polymorphism through method overloading, and run-time polymorphism through method overriding in subclasses. It provides examples of how method overloading and overriding work in Java and the advantages of polymorphism.

Uploaded by

md anik hasan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Spotle.

ai Study Material
Spotle.ai/Learn

Polymorphism
in Java
What Is Polymorphism?

Polymorphism is the concept of


one entity providing multiple
implementations or behaviours.
(Something like an Avatar!)

Spotle.ai Study Material


2
Spotle.ai/Learn
Peter Parker 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.

Spotle.ai Study Material


3
Spotle.ai/Learn
Java And 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.

Spotle.ai Study Material


4
Spotle.ai/Learn
Java Provides 2 Types Of Polymorphism

Compile -Time

Polymorphism

Polymorphism In
Java

Run-Time
Polymorphism

Spotle.ai Study Material


5
Spotle.ai/Learn
Compile-Time Or Static Polymorphism
Compile-time polymorphism 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.
A calculator can add 2 integers. It can also add 2
floats. The addition method adds differently based on
the inputs.

Spotle.ai Study Material


6
Spotle.ai/Learn
Illustration – Method Overloading
Public class PolymorphicCalculator
{
//Add method to add integers
int add (int a, int b)
{
System.out.println(“Adding 2 Integers”);
The Class return a+b;
PolymorphicCalculator }
provides multiple //Another Add method to add floats. Same name //
implementation of the differentiated by data types of parameters and //
method add. return types

float add (float a, float b)


{
System.out.println(“Adding 2 Floats”);
return a+b;
}
}

Spotle.ai Study Material


7
Spotle.ai/Learn
Illustration – Method Overloading
Public static void main(String args[])
{
PolymorphicCalculator calc = new
PolymorphicCalculator ();
//calling the add method with integer
Adding a main method to arguments
Polymorphic Calculator. The System.out.println(calc.add (3,5));
compiler chooses the correct //calling the add method with float arguments
add method to call based on System.out.println(calc.add(3.5,5.6));
the data types of actual }
arguments passed. }

The output will be:


Adding 2 Integers
8
Adding 2 Floats
9.1

Spotle.ai Study Material


8
Spotle.ai/Learn
Run-Time Or Dynamic Polymorphism

Run-time polymorphism 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 Consider a SeniorCitizenAccount class. It
parent’s) to call based on the object will provide a calculateInterest method()
that overrides the calculateInterest()
through which the method is invoked. method in the parent SavingsAccount
class

Spotle.ai Study Material


9
Spotle.ai/Learn
Defining The Base Or Parent Class Method
Public Class 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);
}
}

Spotle.ai Study Material


10
Spotle.ai/Learn
Defining The Overriding Method
Public Class 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);
}
}
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());

SeniorAccount senior = new


SeniorAccoun(10,100000);

//This will call calculateInterest() of parent The output is:


class
Calculating Savings Account Interest.
System.out.println(senior.s.calculateInterest() 6000.0
); Calculating Senior Account Interest.
} 10000.0

Spotle.ai Study Material


12
Spotle.ai/Learn
Advantages Of Polymorphism

Code Ease Of
Cleanliness Implementation

Aligned With Overloaded


Real World Constructors

Reusability And
Extensibility

Spotle.ai Study Material


13
Spotle.ai/Learn

You might also like