JV

Java Fundamentals

19 lessons

Progress0%
1. Introduction to Java
1What is Java?
2. Variables and Data Types
1Primitive Types
3. Control Flow
ConditionalsLoops
4. Methods
Defining MethodsMethod Overloading
5. Object-Oriented Programming
Classes and ObjectsInheritanceInterfaces and Abstract Classes
6. Collections
ArrayList and LinkedListHashMap and HashSet
7. Exception Handling
Checked & Unchecked Exceptionstry-with-resources & Custom Exceptions
8. Generics
Generic Classes & MethodsWildcards & Type Erasure
9. Modern Java
Lambdas & Functional InterfacesStream API & Optional
10. File I/O
java.nio.file APIBuffered I/O & try-with-resources
All Tutorials
JavaMethods
Lesson 6 of 19 min
Chapter 4 · Lesson 2

Method Overloading

Method Overloading in Java

Overloading lets you define multiple methods with the same name but different parameter lists in the same class. Java selects the correct version at compile time based on the argument types and count.

Rules for overloading

  • Parameter lists must differ in number of parameters OR their types.
  • Return type alone is NOT sufficient to distinguish overloads.
  • Access modifiers may differ between overloads.

Varargs Variable-argument methods accept zero or more arguments of a type:

java
public static int sum(int... nums) { … }

Internally, nums is an int[]. A varargs parameter must be the last parameter.

How Java picks the overload

  1. Exact match
  2. Widening conversion (e.g., int → long)
  3. Autoboxing (e.g., int → Integer)
  4. Varargs

Key points:

  • Overloading improves API usability by providing convenient shortcuts.
  • Don't overload to the point of confusion — parameter types should be meaningfully different.

Code Examples

Method overloadingjava
public class Printer {
    public static void print(int value) {
        System.out.println("int: " + value);
    }
    public static void print(double value) {
        System.out.println("double: " + value);
    }
    public static void print(String value) {
        System.out.println("String: " + value);
    }
    public static void print(String label, int value) {
        System.out.println(label + " = " + value);
    }

    public static void main(String[] args) {
        print(42);
        print(3.14);
        print("hello");
        print("score", 100);
    }
}

Java resolves the correct overload at compile time based on the argument types. The name is the same but the signatures differ.

Varargsjava
public class Stats {
    public static int sum(int... nums) {
        int total = 0;
        for (int n : nums) total += n;
        return total;
    }

    public static double average(double... nums) {
        if (nums.length == 0) return 0;
        double total = 0;
        for (double n : nums) total += n;
        return total / nums.length;
    }

    public static void main(String[] args) {
        System.out.println(sum(1, 2, 3));
        System.out.println(sum(10, 20, 30, 40));
        System.out.println(average(4.0, 5.0, 6.0));
    }
}

Varargs (int...) lets callers pass any number of arguments. Internally it is an array, so you can iterate it with for-each.

Quick Quiz

1. Which of the following is a valid basis for method overloading?

2. Where must a varargs parameter appear in a method signature?

Was this lesson helpful?

PreviousNext