Loading...

Java Overloading

Put two methods in one class that both use the name paint. One header is static void paint(String word). The other is static void paint(int n). The bodies are short: the first prints word, the second prints n. From main call paint("ok"); and then paint(3);. The screen should show two lines: ok on the first line and 3 on the second. Java does not treat the second paint as a clash. It treats the two headers as different methods because the parameter lists differ. That shared name with different lists is what this page teaches.

Same name, different lists

Until now each new idea used a fresh method name: ring, greet, twice. Here the name stays paint on purpose. What changes is what sits between the parentheses. One list holds a String. Another list holds an int. The compiler keeps both because those lists are not the same.
Create a small program file named Sign.java. The public class on the first line must match the file name: public class Sign. Do not leave an older Box or Tip class name in this file; the compiler expects the class name and the file name to agree.
Outside main, still inside the class braces, write both paint headers. Keep void on this page: each body prints, and nothing needs to come back to main through return. The first body uses System.out.println(word);. The second uses System.out.println(n);. Save the file before you compile. The full first program is in the next box.

Sign.java with two paint methods

This is the version you compile first. main holds two calls. The class holds two paint methods that share a name.

paint String and paint int

java
public class Sign {
    public static void main(String[] args) {
        paint("ok");
        paint(3);
    }

    static void paint(String word) {
        System.out.println(word);
    }

    static void paint(int n) {
        System.out.println(n);
    }
}

From the folder that holds Sign.java, run javac Sign.java. When parentheses, braces, and semicolons match, javac writes no line of its own. Then run java Sign — not java Sign.java. Leave the .java ending off the second command. The same two commands work on Windows and on macOS. The program should print two lines and stop: ok then 3. It should not ask you to type. If the terminal waits for keyboard input, a nextLine call from an older exercise is still in the file. Delete that call and keep the two paint calls plus the two paint methods above.
Walk the run once in order. Java starts at main. The first call is paint("ok"). The argument is a String, so Java runs the body that takes String word and prints ok. The second call is paint(3). The argument is an int, so Java runs the body that takes int n and prints 3. The name paint is the same both times. The list inside the parentheses is what picks the body.
If both lines show ok, you may have left only the String method and printed a fixed word for every call. If both lines show 3, you may have left only the int method. Keep both headers. If javac reports that paint is already defined, check whether both headers have the same list — for example two copies of paint(String word). Overloading needs a real difference in the list, not a second copy of the same header with a different parameter name alone.

A second list with two words

Parameter count can differ too. Add a third overload that takes two String values and prints them on one line with a space. Leave the first two paint methods in place. Call the new shape from main after the earlier calls, or start a fresh main that focuses on this shape while you learn it.

paint two Strings

java
public class Sign {
    public static void main(String[] args) {
        paint("hi", "sam");
    }

    static void paint(String a, String b) {
        System.out.println(a + " " + b);
    }
}

Compile and run again after you save: javac Sign.java, then java Sign. The screen should show one line: hi sam. The call has two arguments. The method has two parameters. That match is enough for Java to choose this body. If you still have the earlier paint(String word) and paint(int n) in the same class, you can keep them. A call with two String values will not pick the one-argument String method, because the counts differ.
When you put all three overloads in one file for practice, main can call each shape on its own line. A call with one String still goes to paint(String word). A call with one int still goes to paint(int n). A call with two String values goes to paint(String a, String b). The shared name does not erase those distinctions.

Empty list beside a String list

A method with empty parentheses is also a different list. You can keep paint() with no parameters next to paint(String word). The empty call paint(); runs the empty-list body. The call paint("go"); runs the String body.

paint empty and paint String

java
public class Sign {
    public static void main(String[] args) {
        paint();
        paint("go");
    }

    static void paint() {
        System.out.println("ready");
    }

    static void paint(String word) {
        System.out.println(word);
    }
}

After javac Sign.java and java Sign, the screen should show ready on the first line and go on the second. The first call has nothing between the parentheses, so it matches paint(). The second call sends a String, so it matches paint(String word). Renaming one method to paintReady would also compile, but that is a different skill. This page keeps the name paint and lets the lists carry the difference.

Which call runs which header

Read the calls beside the headers. The argument list in the call must fit the parameter list in the header. Types and count both matter on this beginner shape.

📊 call beside matching paint

Call in main Header that runs Screen line
paint("ok"); paint(String word) ok
paint(3); paint(int n) 3
paint("hi", "sam"); paint(String a, String b) hi sam
paint(); paint() ready

A common mix-up is writing two methods that both say paint(String word) and expecting the second one to count as an overload. Changing only the parameter name to paint(String label) does not create a new list for the compiler. The type and the count are what matter here. Another mix-up is calling paint(true) when you never wrote a boolean parameter list. Then javac cannot find a matching paint. Add a header that takes the type you want, or change the call to a type you already support.
Writing System.out.println("paint"); in main prints the word paint. It does not call any method. Keep paint("ok"); when you want the overload that takes a String. Leaving a leftover Box file’s twice method in Sign.java can confuse the screen if main still prints twice(7). Clear main down to the paint calls you are testing, and keep only the paint overloads you need for that run.
Do not nest a paint method inside main’s braces. Methods sit inside the class, next to main. Two overloads are two sibling methods in that same class body.

Before scope

You can keep one method name and several parameter lists in the same class. Java picks the body from the call’s argument list. The screen shows the line from the matching body. Two headers with the same list are not overloads. The next page is scope, where you see which names are visible inside a method and which stay outside.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
5
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top