Method overloading is a feature that allows a class to have more than one method with the same name, but with different parameter lists. It provides a way to define multiple methods that perform similar tasks but with different inputs. This is a key feature in Java, contributing to code readability and reusability.
public class OverloadingExample {
// Method with one parameter
public void display(int a) {
System.out.println("Argument: " + a);
}
// Overloaded method with two parameters
public void display(int a, int b) {
System.out.println("Arguments: " + a + ", " + b);
}
public static void main(String[] args) {
OverloadingExample obj = new OverloadingExample();
obj.display(1); // Output: Argument: 1
obj.display(1, 2); // Output: Arguments: 1, 2
}
}
public class OverloadingExample {
// Method with an integer parameter
public void display(int a) {
System.out.println("Integer: " + a);
}
// Overloaded method with a double parameter
public void display(double a) {
System.out.println("Double: " + a);
}
public static void main(String[] args) {
OverloadingExample obj = new OverloadingExample();
obj.display(1); // Output: Integer: 1
obj.display(1.5); // Output: Double: 1.5
}
}
public class OverloadingExample {
// Method with parameters int and double
public void display(int a, double b) {
System.out.println("Arguments: " + a + ", " + b);
}
// Overloaded method with parameters double and int
public void display(double a, int b) {
System.out.println("Arguments: " + a + ", " + b);
}
public static void main(String[] args) {
OverloadingExample obj = new OverloadingExample();
obj.display(1, 1.5); // Output: Arguments: 1, 1.5
obj.display(1.5, 1); // Output: Arguments: 1.5, 1
}
}
When you pass arguments to overloaded methods, Java can promote them to match the method parameters. For example, if you have an int
and a method accepts a double
, Java can promote the int
to a double
.
public class OverloadingExample {
// Method with double parameter
public void display(double a) {
System.out.println("Double: " + a);
}
public static void main(String[] args) {
OverloadingExample obj = new OverloadingExample();
obj.display(1); // Output: Double: 1.0 (int is promoted to double)
}
}
You can also overload methods that use varargs (variable-length arguments).
public class OverloadingExample {
// Method with varargs
public void display(int... a) {
System.out.println("Varargs: " + Arrays.toString(a));
}
// Overloaded method with regular parameter
public void display(int a) {
System.out.println("Regular: " + a);
}
public static void main(String[] args) {
OverloadingExample obj = new OverloadingExample();
obj.display(1); // Output: Regular: 1
obj.display(1, 2, 3); // Output: Varargs: [1, 2, 3]
}
}
Method overloading works with inheritance as well. You can overload methods in a subclass.
class Parent {
// Method in parent class
public void display(int a) {
System.out.println("Parent: " + a);
}
}
class Child extends Parent {
// Overloaded method in child class
public void display(String a) {
System.out.println("Child: " + a);
}
}
public class OverloadingInheritanceExample {
public static void main(String[] args) {
Child obj = new Child();
obj.display(1); // Output: Parent: 1
obj.display("Hello"); // Output: Child: Hello
}
}
Method overloading is a powerful feature that enhances code readability and reusability. By defining multiple methods with the same name but different parameters, you can handle different types of inputs and operations more efficiently. Understanding method overloading and its various aspects, including type promotion and inheritance, is essential for writing clean and maintainable Java code.