Tuesday, June 2, 2015
Method Overloading
// Methods within the same class that have the same name but slightly different characteristics
// like different number of parameters , different type of parameters that performed the related
// operations are called overloaded methods
// Overloaded method causes ambiquous invocation lead to compile error
public class MethodOverloadingDemo {
public static void main(String[] args) {
Maths math = new Maths();
double res = math.max(2.0, 4.0);
System.out.println(res);
int resu = math.max(2, 4);
System.out.println(resu);
double resul = math.max(2, 4.0);
System.out.println(resul);
}
}
class Maths{
public int max(int x , int y) {
int maximum;
if ( x > y) {
maximum = x;
}else{
maximum = y;
}
System.out.println("\n integer version is called");
return maximum;
}
public double max(double x , double y) {
double maximum;
if ( x > y) {
maximum = x;
}else{
maximum = y;
}
System.out.println("\n double version is called");
return maximum;
}
}
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment