Method Overloading by changing number of parameters.
Overloading in Java, here developer is changing parameters of the program as below.
add.java
[java]package corejava;
public class add {
void sum(int a,int b)
{System.out.println(a+b);
}
void sum(int a,int b,int c)
{System.out.println(a+b+c);}
public static void main(String args[])
{
add obj=new add();
obj.sum(100,100,100);
obj.sum(200,200);
}
}
[/java]
Where developer used two methods with the same name that is void sum() and given different parameters such as int a, int b, int c.
[java]void sum(int a,int b){System.out.println(a+b);}
void sum(int a,int b,int c){System.out.println(a+b+c);} [/java]
Where created one object called
obj, by using this object method has been called.
[java]obj.sum(100,100,100);
obj.sum(200,200); [/java]
When compile the code output will be as follows.
[java]
300
400[/java]
Method Overloading by changing the data type.
Overloading in Java, As explained in the above program where change the data type as double and float then observe the result.
[java]package corejava;
public class add {
void sum(int a,int b)
{
System.out.println(a+b);
}
void sum(double a,float b,int c){
System.out.println(a+b+c);
}
public static void main(String args[])
{
add obj=new add();
obj.sum(100,100,100);
obj.sum(200,200);
}
}
[/java]
Where the developer has changed data type from
integer to
double and
float.
[java]
void sum(int a,int b)
{
System.out.println(a+b);
}
void sum(double a,float b,int c){
System.out.println(a+b+c);
} [/java]
When compile the code following is the result will be generated.
[java]
300.0
400
[/java]
Is method overloading possible by changing the return type.
No, Overloading in Java concept if any one removes the return type, then compile error will become as follows.
[java]package corejava;
public class add {
int sum(int a,int b){
System.out.println(a+b);
}
double sum(double a,float b,int c){
System.out.println(a+b+c);
}
public static void main(String args[]){
add obj=new add();
obj.sum(100,100,100);
obj.sum(200,200);
}
}
[/java]
This code can not be compiled why because it gives compile time error, there is no return type with a name
void.
[java]int sum(int a,int b)
double sum(double a,float b,int c)[/java]
Overloading in Java, main() overloading is possible here as follows.
[java]public class overload {
public static void main(int splesson){
System.out.println(splesson);
}
public static void main(String...args){
System.out.println("Hello SPLessons");
main(78);
}
}
[/java]
In Java one can main method as follows also.
[java]public static void main(String...args)[/java]
Where main() has been invoked, when compile the code output will be as follows.
[java]Hello SPLessons
78[/java]
Overloading vs Overriding in Java
- Overloading occurs at compile-time where as overriding occurs at run time.
- Static methods can be overloaded but where as static methods cannot be overridden.
- Overloading is being done in the same class but where as for overriding base and child classes are required.
- Static binding used by overloading and dynamic binding used by overridden methods.
- Private and Final methods can be overloaded but they cannot be overridden.