Master da Web
Back to Blog

Method overloading in Java

LucasJune 11, 20141 min readUpdated on November 4, 2023
Method overloading in Java

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
Share

Need Hosting or Servers?

Discover our VPS, dedicated servers and professional hosting solutions

Related Articles

View all articles