Task 1 of 7 · 0 solved · 7 to go0%
You can read the problem, but answers are checked only for signed-in students. Sign in to answer →

3.5 Methods — what overloading is

Lesson

Same name, different inputs

Overloading means having several methods with the same name but different parameter lists (different number or types of parameters). It lets one clear name cover related variations, instead of inventing a new name for each.


Worked example

void greet() {
    System.out.println("Hello!");
}
void greet(String name) {
    System.out.println("Hello, " + name + "!");
}

greet();          // Hello!
greet("Ada");     // Hello, Ada!

Two methods named greet — Java tells them apart by what you pass in.


▶ Try it: both greet methods are defined for you. Run this, then add greet("Sam"); and re-run.



What is method overloading?


greet() prints Hello! and greet(String name) prints Hello, name!. Match each call to its output.


True or false?


Type exactly what this program prints.

public class Main {
    static void hi() {
        System.out.println("hi");
    }
    static void hi(String name) {
        System.out.println("hi " + name);
    }
    public static void main(String[] args) {
        hi();
        hi("Ada");
    }
}