Course contentsShow
← Java
Lesson 209 of 2,8705. Methods and Function CallsFree lesson

Overloading by Parameter Type

Overload methods by changing parameter types while keeping the same parameter count.

Overloading by Parameter Type

What you'll learn: You'll discover how to create multiple versions of the same method by changing the types of parameters, even when the number of parameters stays the same.

The Core Idea

You already know you can overload methods by changing how many parameters they have. Now here's another powerful way: keep the same number of parameters, but change their types. Java treats these as completely different methods because the signature (method name + parameter types) is unique.

Think of it like ordering coffee: "small coffee" could mean a small hot coffee or a small iced coffee. Same word "small," but the type of coffee makes them different orders.

How It Works

When you have methods with the same name but different parameter types, Java chooses which one to call based on the types of arguments you pass:

public class Calculator {
    // Overloaded by parameter type - int version
    public static int add(int a, int b) {
        return a + b;
    }
    
    // Overloaded by parameter type - double version
    public static double add(double a, double b) {
        return a + b;
    }
    
    public static void main(String[] args) {
        int result1 = add(5, 3);           // Calls int version → 8
        double result2 = add(5.5, 3.2);    // Calls double version → 8.7
        
        System.out.println(result1);
        System.out.println(result2);
    }
}

Java looks at your arguments (5, 3 vs 5.5, 3.2) and automatically picks the matching method. Same parameter count (two), but different types mean different methods.

Key Takeaway: You can overload methods by changing parameter types while keeping the same number of parameters—Java distinguishes them by the types in the method signature, not just the count.