Method overload
It is possible to create methods with the same name, when the parameters are different!
class CalculateSquare {
public void square() {
System.out.println("No Parameter Method Called");
}
public int square(int number) {
return number * number;
}
public float square(float number) {
return number * number;
}
public double square(double number) {
return number * number;
}
public static void main(String[] args) {
CalculateSquare obj = new CalculateSquare();
obj.square();
obj.square(5);
obj.square(2.5);
}
}So these methods have the same name square but because their parameters are different they are seen as different mathods. When one of the methods gets called, then relevant methods will be found based on the argument. Fx
This will be directed to this method, becaus we are callin the method with an integer
The same can be done with a constructor!
Here we use the different constructors
Last updated
Was this helpful?