CyberStars

Method Overloading

Java lets you have multiple methods with the same name, as long as they take different parameters. This is called method overloading

Imagine Phil Cassidy's gun shop. You walk in and say "I want a weapon." Phil's response depends on what info you give him:

  • Just a name? He grabs it off the shelf
  • A name and a quantity? He grabs that many
  • A name, quantity, and budget? He checks the price too

Same request ("I want a weapon"), different details. That's overloading

public class Main {
    public static void weapon(String name) {
        System.out.println("Weapon: " + name);
    }

    public static void weapon(String name, int count) {
        System.out.println("Weapon: " + name + " (x" + count + ")");
    }

    public static void main(String[] args) {
        weapon("Shotgun");
        weapon("Pistol", 3);
    }
}

Output

Weapon: Shotgun
Weapon: Pistol (x3)

Java looks at how many arguments you pass and what types they are, then picks the right version of the method. This happens at compile time: Java figures it out before the program even runs

Let's trace the two calls:

  • weapon("Shotgun") has a single String argument, so Java picks the first version and prints Weapon: Shotgun
  • weapon("Pistol", 3) has a String and an int, so Java picks the second version and prints Weapon: Pistol (x3)

The key rule: overloaded methods must differ in their parameter list. That means either a different number of parameters, or different types

public class Main {
    public static void info(String text) {
        System.out.println("Text: " + text);
    }

    public static void info(int number) {
        System.out.println("Number: " + number);
    }

    public static void info(String text, int number) {
        System.out.println("Text: " + text + ", Number: " + number);
    }

    public static void main(String[] args) {
        info("Tommy");
        info(42);
        info("Lance", 100);
    }
}

Output

Text: Tommy
Number: 42
Text: Lance, Number: 100

Three methods, all named info, but Java knows which one to call based on what you pass in


What does NOT count as overloading? Changing only the return type

public class Main {
    // This WON'T compile: same parameters, different return type
    public static int calculate(int a) { return a * 2; }
    public static double calculate(int a) { return a * 2.0; }
}

Java says: "If someone calls calculate(5), which one do I pick?" It can't tell just from the return type, so this is not allowed


Overloading is super common in Java's built-in libraries. You've already used it without knowing: System.out.println is overloaded! It can take a String, an int, a double, a boolean... all different versions of the same method

public class Main {
    public static void main(String[] args) {
        System.out.println("hello");   // println(String)
        System.out.println(42);        // println(int)
        System.out.println(3.14);      // println(double)
        System.out.println(true);      // println(boolean)
    }
}

Mission: Phil's Orders

Phil Cassidy keeps track of weapon orders. He wants to print an order with more or less detail, depending on what information he has

Write three overloaded methods called describe:

  • one that takes only the weapon name and prints Weapon: followed by the name, for example Weapon: Sniper
  • one that takes the name and how many pieces are ordered and adds the number followed by ordered, for example Weapon: Sniper - 4 ordered
  • one that takes the name, how many pieces, and the price of one piece, computes the total (price times quantity), and prints the price, the quantity, and the total, for example Weapon: Sniper - $10 x 4 ordered - $40

In main, store the weapon name in weapon, the quantity in quantity, and the unit price in price, then make the three calls using those variables: describe(weapon), describe(weapon, quantity), and describe(weapon, quantity, price)

Example

Weapon: Sniper
Weapon: Sniper - 4 ordered
Weapon: Sniper - $10 x 4 ordered - $40