header

header

IT Training on JAVA | 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.
package com.softmacs.polymorphism;

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.
Compile time polymorphism (static binding or method overloading)
Runtime polymorphism (dynamic binding or method overriding)
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
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