Overloading:
If at all we have the same function more than once
in a class with different arguments (may be no or type of arguments) then that
concept is known as overloading.
Note:
While calling any function we can call the
required values.
A function can return a value, if at all we want
to return a value in the place of void we need to write data type of function
and in the body we must write a return statement.
Develop a java program which describes
overloading concept
package pack1;
public class overloadingeg
{
// public static int i,j;
public static int add(int x,int y){
return x+y; // method overloading
}
public static int add(int x,int y,int z){
return x+y+z; // method overloading
}
public static void main(String[] args) {
int i=add(10,20);
int j=add(20,30,40);
System.out.println("sum of 10 & 20:"+i);
System.out.println("sum of 20,30 & 40:"+j);
}
}
Long
package pack2;
public class overloading {
public static long add(long a,long b){
long res=a+b;
return res;
}
public static long add(long a,long b,long c){
long res=a+b+c;
return res;
}
public static void main(String[] args) {
long var1=add(10,20);
long var2=add(10,20,30);
System.out.println(var1);
System.out.println(var2);
}
}
Double
public class overloading {
public static double add(double d,double e){
double res=d+e;
return res;
}
public static long add(long a,long b,long c){
long res=a+b+c;
return res;
}
public static void main(String[] args) {
double var1=add(0.5,0.6);
long var2=add(10,20,30);
System.out.println(var1);
System.out.println(var2);
}
}
No comments:
Post a Comment