What is Polymorphism in java?
Simple
meaning of Polymorphism is more
than one form.
The word Polymorphism
is derived from two Greek words i.e., poly and morphs.
The word
poly mean many/more and morphs means forms.
Types of Polymorphism in Java?
There are two
types of Polymorphism in Java.
1. Compile
Time Polymorphism.
2. Run Time
Polymorphism.
What is Compile-Time Polymorphism in Java?
Compile time
polymorphism or static method dispatch is a process in which a call to an overloading
method is resolved at compile time rather than at run time, in this process overloaded
method is called through reference variable of current class here no need of
super-class concept.
How to achieve Polymorphism in Java?
We can achieve Polymorphism in Java by two ways i.e., method overloading and method overriding.
What is Method Overloading in Java?
If a class
contains multiple methods with same name but different parameters and with different
parameter data types then it is known as Method Overloading.
Advantage of method overloading?
Increase the
readability of the program.
Rule to overload a method?
1. You must
change the number of arguments.
2. You must
change the data type.
Example of method overloading.
· By
changing the number of argument.
Class Sport{
void players(String
player1, String player2){
System.out.println("Players with two argument
invoked");
}
void players(String player1, String player2, String player3){
System.out.println("Players with three
argument invoked");
}
}
· By
changing data types.
Class Sport{
void players(String player1, int wickets){
System.out.println("Players with two argument
invoked");
}
void players(String player1, double runs){
System.out.println("Players with three argument invoked");
}
}
What is Run-Time Polymorphism in Java?
Run-time
polymorphism is a process in which we have to override a method and call to
that overridden method is resolved at runtime rather than at compile-time.
In the process
of Run-Time polymorphism, an overridden method is called through the super
class reference variable. However determination of the method to be called is
based on the object of class that is being referred by the reference variable.
Ex- Super-Class sc=new Child-Class();.
Here Super-Class reference variable is sc that is referred to the Child-Class object i.e., here Child-Class object will be created using the
reference of Super-Class sc, This is
the concept of up-casting in Java.
Up-Casting
If parent
class reference variable refers to the object of child class then it is nothing
but up-casting in Java , See example given below using class and interface type
reference .
Class type reference variable.
Class X{}
Class Y extends X{}
X obj=new Y();//up-casting
Interface type reference variable.
Interface I{}
Class X{}
Class Y extends X implements I{}
X obj=new Y();
I obj=new Y();
X is-a Y
X is a I
X is a Object
Refer this concept : Since Object is the father class of all
classes in Java , So we can say X is a Object.
Runtime Polymorphism Example in Java.
Here in this
example we have created a class Sport with a method name as myMethod()
that printing just a text as In Sport Class, then we have created a class
cricket which extends Sport class and override the myMethod() method of
Sport Class and providing implementation according to our requirement, after
that we have created a class Football which also extends Sport and providing implementation
according to requirement.
Now we have created a class Testmain with main method and we are calling myMethod() by the reference variable of parent class i.e., Sport class. Since it refers to the child class objects and child class overrides the parent class(Sport class) method i.e., myMethod() and child class method is invoked at run time.
NOTE :
Method to be called/invoked is determined by the JVM not by the compiler because
compiler checks only reference type, it
is known as runtime polymorphism.
class Sport{
void myMethod(){
System.out.println("In Sport Class");
}//end of method
}//end of classs
class Cricket extends Sport{
void myMethod (){
System.out.println("Playing Cricket");
}//end of method
}//end of classs
class Football extends Sport{
void myMethod (){
System.out.println("Playing Football");
}//end of method
}//end of class
public class Testmain {
public static void main(String[] args){
Sport s;
s=new Cricket();
s. myMethod();
s=new Football();
s. myMethod();
}
}
output:
Playing Cricket
Playing Football
output:
Playing Cricket
Playing Football
Java Polymorphism Example With Bank.
Here we have
created a class Bank similar to previous example of Sport class with respect to
logic, here classes BOI, CanraBank and BOB extending Bank and providing
specific implementation according to different requirement i.e., different Rate
of Interest.
Now we have
created a class Testmain with main method and we are calling getROI () by the
reference variable of parent class i.e., Bank class. Since it refers to the
child class objects and child class overrides the parent class (Bank class)
method i.e., getROI () and child class method is invoked at run time,
see example and try to understand.
class Bank{
float getROI(){return 0;}
}
class BOI extends Bank{
float
getROI(){return 8.4f;}
}
class CanaraBank extends Bank{
float
getROI(){return 7.3f;}
}
class BOB extends Bank{
float
getROI(){return 9.7f;}
}
public class Testmain {
public static void main(String[] args){
Bank b;
b=new BOI();
System.out.println("BOI Rate of Interest: "+b. getROI());
b=new CanaraBank();
System.out.println("Canara Bank Rate of Interest: "+b.
getROI());
b=new BOB();
System.out.println("BOB Rate of Interest: "+b. getROI());
}
}
output:
BOI Rate of Interest: 8.4
CanaraBank Rate of Interest:7.3
BOB Rate of Interest:9.7
output:
BOI Rate of Interest: 8.4
CanaraBank Rate of Interest:7.3
BOB Rate of Interest:9.7
No comments