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.
Rule of overloading a method in Java
Following rules are to be followed in method
overloading
- The first rule is to change method signature
in method overloading. Method signature is made of (1) number of arguments,
(2) type of arguments and (3) order of arguments if they are of different
types.
- The name of method should be same for the
overloaded methods.
- The return type of method is not part of
method signature, so just changing the return type will not overload method
in Java.
Advantages of method overloading in java
- Overloading in Java is the ability to
create multiple methods of the same name, but with different parameters.
- The main advantage of this is cleanliness
of code.
- Method overloading increases the
readability of the program.
- Overloaded methods give programmers the
flexibility to call a similar method for different types of data.
-
Overloading is also used on constructors to create new objects given
different amounts of data.
-
You must
define a return type for each overloaded method. Methods can have different
return types
Different Ways to overload method in Java
- By changing the no. of arguments.
- By changing the data types.
In this example, we have created four
overloaded methods (first three method overload by changing no. of arguments and
last one method overloaded by changing there data type).
-
- class Ovrld {
- void check() {
- System.out.println("No parameters");
- }
-
- void check(int a) {
- System.out.println("a: " + a);
- }
-
- void check(int a, int b) {
- System.out.println("a and b: " + a + " " + b);
- }
-
- double check(double a) {
- System.out.println("double a: " + a);
- return a * a;
- }
- }
Now we create a main class
Overload, which prints all overloaded
methods.
- class Overload {
- public static void main(String args[]) {
- Ovrld ob = new Ovrld();
- double result;
-
- ob.check();
- ob.check(10);
- ob.check(10, 20);
- result = ob.check(123.2);
- System.out.println("Result of ob.check(123.2): " + result);
- }
- }
Output