In Java you can declare methods with the same name in a class, but each method must contain different sets of parameters. In other words, this is called Method Overloading.
public class Calculadora{ public int soma(int valor1, int valor2){ return valor1 + valor2; } public float soma(float valor1, float valor2){ return valor1 + valor2; } }
When executing the “sum” method, if we pass two integer values as parameters, the sum method of type INT will be executed; if the parameters are two floating point values, the second sum method of type FLOAT will be executed.
- Tags:
- java