Introduction
In this article, we will discuss method overloading in Java. We also discuss method overloading sub-parts why we need that, their rules, properties, advantages and the different ways to overload methods.
What is method?
A method is a collection of codes to perform an operation.
Creating a Method
In general form, method has following
properties
modifier returnValueType methodName(list of parameters/arguments)
{
// Method body;
}
Method have following parts
- Access specifier It is optional as it tells the compiler how to call public, private, and others specifier.
- Return Type: sometimes a method may return a value or does not return a value (for that we use void).
- Name of method: This is to assign name of method.
- Parameters: a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses. If there are no parameters, you must use empty parentheses.
- Body of method: The method body enclosed between braces contains a collection of statements that define what the method does.
What is Method Overloading?
In Java when we define two methods with the same name but have different parameters. These methods are said to be overloaded, and the process is referred to as method overloading. Let's take an
example
- Define a method check() which contain
single parameter.
void check(int a) - In this example, we have defined a method
check() with a single parameter.
void check(int a, int b) - This example contains a method with the same name called check() which contains two parameters. Hence both the methods are overloaded as they have same name (check()) with different parameters.
Method overloading is one of the ways in
Java supports compile-time polymorphism.
What's the need for method overloading?
- Overloading is very useful in Java. In overloading we can overload methods as long as the type or number of parameters (arguments) differ for each of the methods. Suppose you need to perform a complex mathematical operation, but sometimes need to do it with two numbers and sometimes three, etc.
- In overloading a class can have multiple methods with the same name that can be differentiated by the number and type of arguments passed into the method. For example, to simplify your calls, by using the method with the least number of arguments when all defaults are acceptable. Or you can use it when you have more than one way of identifying the same thing, like (String) name and (Long) ID. In these two examples, once the overloaded methods accomplish initialization, whatever job needs to be finished can be done by a common method.
- Method Overriding is the concept which implements Dynamic Code (Method) binding. i.e. The method which is going to bind or execute is decided at the runtime depending on the Object that we use to call that method. Assume we have two Classes Demo1 and Demo2 where class Demo2 inherits a class Demo1. And after we have overridden a method of class Demo1 in class Demo2. Now the criteria is that which method (superclass method or subclass overridden method) is to be called or executed is determined based on the object (class Demo1 object Or class Demo2 object) we are using to call that method.
Method overloading properties in Java
- In overloading, methods are binded During compilation process called static binding, compiler bind method call to actual method.
- The overloaded methods are fast because they are bonded during compile time and no check or binding is required during runtime.
- Necessary rule of overloading in Java is that two overloaded method must have a different signature. The below points brief what does method signature means in Java
- Return type of method is not part of method signature in Java.
- Number of argument to a method is part of method signature.
- Type of argument to a method is also part of a method signature
- Order of argument also forms part of method signature provided they are of different type.


Sam HobbsPosted Apr 12, 2013, 1:30 PM
In C++ templates can SOMETIMES be a useful alternative to overloading, correct? In Java, would generics be a useful alternative to overloading? My quick research indicates that Java generics are not as useful as C++ templates.