+91 81900 98289

Method Overloading

Overloading means two methods share a name and differ in the parameter list. The compiler picks the version that matches the arguments you pass. The return type alone is not enough to tell them apart.

Use overloading when the action is the same and only the input shape changes, such as printing an int or printing a String. If the actions differ, give them different names.

Example

The int call uses the first method. The string call uses the second. The printed prefix shows which one ran.

public class Show { static void line(int n) { System.out.println("n=" + n); } static void line(String text) { System.out.println("t=" + text); } public static void main(String[] args) { line(4); line("Java"); } }
line(4) cannot call the String version. 4 is an int. A method named line that also takes one int would be a duplicate, and javac would reject the class.