Question #262284

 Different methods named sum need to be created in one class, explain what mechanism should be used. Then Illustrate with examples the different ways to achieve that.


Expert's answer

The mechanism used in this case is method overloading.

it can be achieved through the following:

a. Changing number of arguments

b. Changing the data type


Example

class Addition{  
  static int sum(int a,int b){
    return a+b;
  }  
//Changing number of arguments
   static int sum(int a,int b,int c){
      return a+b+c;
  } 
// Changing the data type
    static double sum(double a,double b,double c){
      return a+b+c;
  }          
}  
LATEST TUTORIALS
APPROVED BY CLIENTS