Syntax
returnType methodName(paramsA) { }\nreturnType methodName(paramsB) { }Examples
Overloading by Parameter Count
Multiple versions of a method accepting a different number of arguments.
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public int add(int a, int b, int c) {
return a + b + c;
}
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(2, 3)); // 5
System.out.println(calc.add(2, 3, 4)); // 9
}
}Overloading by Parameter Type
Versions of a method that accept different types of arguments.
public class Printer {
public void display(int value) {
System.out.println("Integer: " + value);
}
public void display(String value) {
System.out.println("String: " + value);
}
public void display(double value) {
System.out.println("Double: " + value);
}
public static void main(String[] args) {
Printer p = new Printer();
p.display(42); // Integer: 42
p.display("hello"); // String: hello
p.display(3.14); // Double: 3.14
}
}Overloaded Constructors
Overloading applies to constructors too, providing multiple ways to build an object.
public class Pizza {
private String size;
private int toppingCount;
public Pizza() {
this("Medium", 0);
}
public Pizza(String size) {
this(size, 0);
}
public Pizza(String size, int toppingCount) {
this.size = size;
this.toppingCount = toppingCount;
}
public void describe() {
System.out.println(size + " pizza with " + toppingCount + " toppings");
}
}
new Pizza().describe(); // Medium pizza with 0 toppings
new Pizza("Large", 3).describe(); // Large pizza with 3 toppingsBest practices
- Only overload methods when the different versions perform genuinely similar, related operations - otherwise use distinct method names for clarity
- Avoid overloads that are ambiguous with automatic type widening (like both int and long versions), since the compiler may not resolve calls the way you expect
- Remember overload resolution happens at compile time based on the declared parameter types, not the runtime type of the arguments
- Use overloaded constructors together with this() chaining to avoid duplicating initialization logic across each version
At a glance
- Purpose
- General-purpose application development
- File extension
- .java
- Runs in
- Java Virtual Machine
- Usually used with
- JDK and Java libraries
Specifications & further reading
Related Java documentation
Classes
Classes in Java are blueprints for creating objects, defining their properties (fields) and behaviors (methods). Java is a strictly object-oriented language where everything is encapsulated within classes. A class serves as a template that specifies what data an object will store and what operations it can perform.Interfaces
An interface defines a contract of methods that implementing classes must provide, without specifying how those methods work internally. Unlike a class, an interface cannot be instantiated directly - it only declares what a class can do, not how. A class uses the 'implements' keyword to fulfill an interface's contract, and a single class can implement multiple interfaces, which is Java's way of achieving a form of multiple inheritance.Abstract Classes
An abstract class is a class that cannot be instantiated directly and may contain both fully implemented methods and abstract methods (declared without a body, which subclasses must implement). Abstract classes sit between interfaces and regular classes: like an interface, they define a contract; like a regular class, they can hold state (fields) and provide shared, already-implemented behavior.Constructors
A constructor is a special method that runs automatically when an object is created with 'new', typically used to initialize the object's fields. A constructor shares its name with the class and has no return type, not even void. Java supports constructor overloading, letting a class offer multiple ways to construct an object, and constructor chaining with this(...), where one constructor calls another in the same class.
Classes in Java are blueprints for creating objects, defining their properties (fields) and behaviors (methods). Java is a strictly object-oriented language where everything is encapsulated within classes. A class serves as a template that specifies what data an object will store and what operations it can perform.Interfaces
An interface defines a contract of methods that implementing classes must provide, without specifying how those methods work internally. Unlike a class, an interface cannot be instantiated directly - it only declares what a class can do, not how. A class uses the 'implements' keyword to fulfill an interface's contract, and a single class can implement multiple interfaces, which is Java's way of achieving a form of multiple inheritance.Abstract Classes
An abstract class is a class that cannot be instantiated directly and may contain both fully implemented methods and abstract methods (declared without a body, which subclasses must implement). Abstract classes sit between interfaces and regular classes: like an interface, they define a contract; like a regular class, they can hold state (fields) and provide shared, already-implemented behavior.Constructors
A constructor is a special method that runs automatically when an object is created with 'new', typically used to initialize the object's fields. A constructor shares its name with the class and has no return type, not even void. Java supports constructor overloading, letting a class offer multiple ways to construct an object, and constructor chaining with this(...), where one constructor calls another in the same class.