Last Updated: 25 December, 2023
In a Java class, writing two or more methods with the same method name but different parameters (which may be different in number of parameters or type of parameters, or both). There method are called overloaded methods and this feature is called method overloading.
Method overloading is an implementation approach for Compile-Time Polymorphism.
Overloaded methods must be declared within the same class.
The overloaded method must be different in the argument list (number of parameters, data type of parameters, or order of parameters).
The overloaded method can have the same or a different return type.
Overloaded methods can have different access modifiers (public, private, or protected).
Overloaded methods can throw different exceptions.
For example method overloading:
Here, the sum() method is overloaded in the Calculation class. These methods have the same name but have different parameters. The return types of overloaded methods may be the same or different.
Method Overloading provides the following benefits in Java Programming.
Method Overloading increases the readability of the program.
Method Overloading reduces the execution time because the binding is done in compilation time itself.
Method Overloading is a flexible approach for writing or calling the same method with different parameters.
Example 1: Method Overloading - By changing the number of parameters
In the above example, the class Addition has three overloaded add() methods, and each method has a different number of int parameters. These methods are called based on their number of parameters. See the output below.
Output
Added two int values: 60
Added three int values: 90
Added four int values: 220
Example 2: Method Overloading - By changing the data types of parameters
Here, the Addition class has three overloaded add methods, and each add method has two parameters, but each method's parameter types are different from each other, so the compiler will call these methods according to their data types at compile time.
Output
Added two int values: 60
Added two float values: 66.25
Added one int and one float value: 110.25
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
Why is method overloading a compile-time polymorphism in Java?
In method overloading, a class has more than one method with the same method name but different parameters (which may be different in number of parameters, type of parameters, or both). At the time of the compilation of the Java program, the compiler examines the method signature and determines which method to invoke for a given method call. Since it is exhibited during compile time, it is called compile-time polymorphism.
Can we synchronize overloaded methods in Java?
Yes, overloaded methods can be synchronized in Java.
Can we declare overloaded methods as final in Java?
Yes, we can declare overloaded methods as final in Java.
Can we overload static methods in Java?
Yes, we can overload static methods in Java but we cannot override them.
The main() method can be overloaded in Java?
Yes, we can overload the main() method. A class can have any number of main() methods, but execution starts from the public static void main(String[] args) only.
What will happen if we write two methods in a class with the same method signature but different return types?
The compiler will give a duplicate method error. The compiler checks only the method signature for duplication, not the return types. If two methods have the same method signature, straight away it gives a compile-time error.
How do compilers differentiate overloaded methods from duplicate methods?
The compiler uses a method signature to check whether the method is overloaded or duplicated. Duplicate methods will have the same method signatures, i.e., the same name, the same number of arguments, and the same types of arguments. Overloaded methods will also have the same name but differ in a number of arguments or other types of arguments.
Is it possible to declare one overloaded method as static and another one as non-static?
Yes. It's possible because overloaded methods can be either static or non-static.
Can an overloaded method be overridden?
Yes, we can easily override an overloaded method in sub-clas, which is overloaded in super class.
Can we overload constructors in Java?
Yes, we can overload constructors in Java.