POLYMORPHISM

What is Polymorphism ?

Polymorphism is an important Object Oriented concept and widely used in all Object Oriented Programming Language.
Polymorphism word comes from ancient Greek where poly means many.Polymorphism means many form.
Polymorphism in Java means same object behave differently in different place.

For e.g Lady is one of the example polymorphism.Same Lady object will be behave differently when she is sister, wife and mother.

1
2
3
4
5
6
7
8
9
10
11
12
public class AccountClass {
    public String accountopen(){
        return "Account Opened";
}
class SavingAccountClass extends AccountClass{
    public String accountopen(){
        return "Saving Account Opened";
class CurrentAccountClass extends AccountClass{
    public String accountopen(){
        return "Current Account Opened";
    }
}
 
There are two kind of polymorphism
1.Runtime polymorphism (dynamic binding or method overriding)
In this flow of the method will be decided the run time of the code.
2.Compile time polymorphism (static binding or method overloading)
In this flow of the method will be decided the compile time of the code.
 
Method Overloading
When Java Programmer declare two methods of the same name but with different signature in a same class,this concept is called as Method overloading.
Different signature means change in number of arguments or change in type of arguments.
System.out.println() is popular example of method overloading because printly() method takes string,int,float or any other data type.
 
Few points Method Overloading
Overloaded method are loaded and bonded at compile time.
Overloaded methods are fast because they bonded at compile time.
 
What do you mean by different method signature for method Overloading in Java
If number of argument is different.
If type of argument is different.
If sequence of argument is different ,only when arguments of different types
 
1
2
3
4
5
Class A{
void method(int a, string x){
}
void method(string x,int a){
}

return type of method is not part of method signature in java.