Tuesday, September 15, 2015

Method Overloading in java

Methods of the same name can be declared in the same class, as long as they have different

sets of parameters (determined by the number, types and order of the parameters)—this

is called method overloading. When an overloaded method is called, the compiler selects



the appropriate method by examining the number, types and order of the arguments in

the call. Method overloading is commonly used to create several methods with the same

name that perform the same or similar tasks, but on different types or different numbers



of arguments. For example,

Declaring Overloaded Methods


 

public class Apples {



public static void main(String[] args){

System.out.printf("square of integer 7 is %d \n", square(7));

System.out.printf("square of double 7.5 is %f \n", square(7.5));



}

public static int square (int intValue){

return intValue*intValue;


}

public static double square (double doubleValue){

return doubleValue*doubleValue;


}

}
 

1 comment: