Wednesday, February 1, 2017
Method overloading in java
Method overloading in java
Method overloading in java is the facility which allow us to write same name method with different arguments and hence different implementation.So now with the help of method overloading we can use same method name with different implementation and method overloading should we within same class. Let me explain you with the help of an example.
In the above code we can see that display ( ) is overloaded with different arguments.
Important point regarding overloading
1> If we are passing int value then it will search for any overloaded method with integer argument, if not available then it will search for any method with its just higher type like long and again if it is also not available then it will search for its wrapper class definition Integer .For ex-
Rules to be followed while doing overloading
1> Method name must be same.
2> Return type can be different.
3> Modifier can be different.
4> Parameters must be different as follows.
a> Types of parameter.
b> Number of parameter.
c> Order of parameters type.
Note 1 : Method overloading will be used to achieve static / compile type ploymorphism.
Note 2 : Try to execute the program passing byte value and defining overloaded with byte type , short type, int type, long type and lastly its wrapper type then if
public class prog{
void display() {
System.out.println("Method without argument") ;
}
void display(int x) {
System.out.println("X="+x) ;
}
void display(String x){
System.out.println("X="+x) ;
}
void display(String y, int x){
System.out.println(y) ;
System.out.println(x) ;
}
public static void main(String sush[ ]) {
prog p = new prog();
p.display( ) ;
p.display(10) ;
p.display("sushant") ;
p.display("sush",20) ;
}
}
In the above code we can see that display ( ) is overloaded with different arguments.
Important point regarding overloading
1> If we are passing int value then it will search for any overloaded method with integer argument, if not available then it will search for any method with its just higher type like long and again if it is also not available then it will search for its wrapper class definition Integer .For ex-
public class prog{
void display(Integer x){
System.out.println("Integer="+x);
}
void display(long x){
System.out.println("Long="+x);
}
public static void main(String sush[ ]) {
prog p = new prog();
p.display(10);
}
}
In the above example display method is overloaded with Integer (wrapper class) and long as an argument and from main( ) we had passed the value as int type then it will search for it wider type i.e long , if available then it will execute else it will search for its wrapper class.
Rules to be followed while doing overloading
1> Method name must be same.
2> Return type can be different.
3> Modifier can be different.
4> Parameters must be different as follows.
a> Types of parameter.
b> Number of parameter.
c> Order of parameters type.
Note 1 : Method overloading will be used to achieve static / compile type ploymorphism.
Note 2 : Try to execute the program passing byte value and defining overloaded with byte type , short type, int type, long type and lastly its wrapper type then if
a> Overloaded method with byte type is available then it will execute that method only and if not available then it will search for its just higher i.e short .
b> short is also not available then it will search for its more higher type i.e int type. If available then it will execute.
c> int is not available then it will search for its more n more higher type i.e long . If available then it will execute.
d> long is not available then it will search for overloaded method with its wrapper type i.e Byte. If available then it will execute.
e> Wrapper class in not available then it will search for VAR-ARGS ( Variable arguments ) . To know more about VAR-ARGS Click Here ! .
e> Wrapper class in not available then it will search for VAR-ARGS ( Variable arguments ) . To know more about VAR-ARGS Click Here ! .
Available link for download
Labels:
in,
java,
method,
overloading