Java Method Overloading Example - How to Overload Methods

Source
en-orig
Oct 17, 2017 May 4, 2026
Video preview
Share:

Method overloading allows a class to have multiple methods with the same name as long as their parameter lists differ. The method signature consists of the method name and the number and type of parameters, while the return type has no effect on overloading.

Definition and Method Signature ⏱ 0:00

  • Method overloading lets a class have more than one method with the same name
  • Parameter lists must be different between overloaded methods
  • Method signature includes method name and the number and type of its parameters
  • Return type has no influence on method overloading
  • Example with Three Overloaded Add Methods ⏱ 0:30

  • Created method public static int add(int a, int b) that returns a + b
  • Created method public static int add(int a, int b, int c) that returns a + b + c
  • Created method public static String add(String a, String b) that returns a + b
  • All three methods have the same name "add" but differ in number and type of parameters
  • Binding at Runtime and Return Type Rule ⏱ 2:04

  • add(1, 2) binds to the two-int method, returns 3
  • add(1, 2, 3) binds to the three-int method, returns 6
  • add("hello", "world") binds to the two-String method, returns "hello world"
  • Return type alone cannot differentiate overloaded methods; same parameters with different return type causes error
  • Key Takeaways

  • Overloading requires the same method name but different parameter lists (number or type).
  • Method signature includes name and parameter types, not return type.
  • Return type does not affect overloading; only the method signature matters.
  • The compiler selects the correct overloaded method based on the arguments provided at the call site.
  • Conclusion

    Method overloading provides flexibility to define multiple methods with the same name but different parameters, improving code readability and reusability.

    Visual Highlightsbeta