Java Polymorphism (Part 4)
Java Polymorphism
Polymorphism means "many forms", and it occurs when we have many classes that are related to each other by inheritance.
Like we specified in the previous chapter; Inheritance lets us inherit attributes and methods from another class. Polymorphism uses those methods to perform different tasks. This allows us to perform a single action in different ways.
Why to use polymorphism ?
▪ Single variable can be used to store multiple data types.
▪ Easy to debug the codes
There are 2 types of polymorphism :
1. Compile-time polymorphism
eg:- Here I am declaring add( ) which adds two values and overloading it with different parameters.
class
Main
{ public void
add
(int
num
) { System.out.println("
Addition of
" +
num
+ "
and 0 is
:
" +
num
); } public void
add
(int
num
, int
num1
) { System.out.println("
Addition of
" +
num
+ "
and
" +
num1
+ "
is :
"+ (
num + num1
)); } public void
add
(double
num
, double
num1
) { System.out.println("
Addition of
" +
num
+ "
and
" +
num1
+ "
is :
"+ (
num
+
num1
)); } } public class
MyClass
{ public static void main(String[] args) { Main
obj
= new Main();
obj
.
add
(5);
obj
.
add
(5, 10);
obj
.
add
(5.5, 10.5); } }
Output:-
Addition of 5 and 0 is : 5
Addition of 5 and 10 is : 15
Addition of 5.5 and 10.5 is : 16.0
This is an example of compile-time polymorphism as when we pass arguments into methods it is decided at compile-time that which method should be called.
2. Run-time polymorphism
//creating a parent class
class
Bank
{ public double
rateOfInterest
() { return
0
; } }
// creating child classes
class
SBI extends Bank
{ public double
rateOfInterest
() { return
7.8
; } } class
AXIS extends Bank
{ public double
rateOfInterest
() { return
8.2
; } } class
HDFC extends Bank
{ public double
rateOfInterest
() { return
8.6
; } } public class
MyClass
{ public static void main(String[] args) { SBI
s
= new SBI(); AXIS
a
= new AXIS(); HDFC
h
= new HDFC(); System.out.println("
Rate of Interest of SBI:
" +
s
.
rateOfInterest
()); System.out.println("
Rate of Interest of AXIS:
" +
a
.
rateOfInterest
()); System.out.println("
Rate of Interest of HDFC:
" +
h
.
rateOfInterest
()); } }
Rate of Interest of SBI: 7.8
Rate of Interest of AXIS: 8.2
Rate of Interest of HDFC: 8.6
Difference between Compile-time Polymorphism and Run-time Polymorphism
Compile-time Polymorphism | Run-time Polymorphism |
---|---|
In Compile time Polymorphism, the call is resolved by the compiler. | In Run time Polymorphism, the call is not resolved by the compiler. |
It is also known as Static binding, Early binding and overloading as well. | It is also known as Dynamic binding, Late binding and overriding as well. |
Method overloading is the compile-time polymorphism where more than one methods share the same name with different parameters or signature and different return type. | Method overriding is the runtime polymorphism having same method with same parameters or signature, but associated in different classes. |
It provides fast execution because the method that needs to be executed is known early at the compile time. | It provides slow execution as compare to early binding because the method that needs to be executed is known at the runtime. |
Compile time polymorphism is less flexible as all things execute at compile time. |
Run time polymorphism is more flexible as all things execute at run time. |