Definition of Overloading Methods:-
Overloading Methods Program:-
public class ABC{
int a, b, c;
public void setValue(){
a=2;
b=4;
c=6;
}
public void setValue(int a){
this.a=a;
b=4;
c=6;
}
public void setValue(int a, int b){
this.a=a;
this.b=b;
c=6;
}
public int setValue(int a, int b, int c){
this.a=a;
this.b=b;
this.c=c;
int z = a+b+c;
return z;
}
}
We can declare several methods of same name in a class, which is known as methods overloading. For overloading methods argument lists must be different and return type can be different. In the following code, there are four methods of same name with different argument, which is an example of overloading methods.
Overloading Methods Program:-
public class ABC{
int a, b, c;
public void setValue(){
a=2;
b=4;
c=6;
}
public void setValue(int a){
this.a=a;
b=4;
c=6;
}
public void setValue(int a, int b){
this.a=a;
this.b=b;
c=6;
}
public int setValue(int a, int b, int c){
this.a=a;
this.b=b;
this.c=c;
int z = a+b+c;
return z;
}
}