Method Overloading In Java

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?

  1. 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.
  2. 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.
  3. 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
  1. 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.
  2. The name of method should be same for the overloaded methods.
  3. 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

  1. Overloading in Java is the ability to create multiple methods of the same name, but with different parameters.
  2. The main advantage of this is cleanliness of code.
  3. Method overloading increases the readability of the program.
  4. Overloaded methods give programmers the flexibility to call a similar method for different types of data.
  5. Overloading is also used on constructors to create new objects given different amounts of data.
  6. You must define a return type for each overloaded method. Methods can have different return types

Different Ways to overload method in Java

  1. By changing the no. of arguments.
  2. 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).
  1. //method overloading.  
  2. class Ovrld {  
  3.     void check() {  
  4.         System.out.println("No parameters");  
  5.     }  
  6.     // Overload check for single integer parameter  
  7.     void check(int a) {  
  8.         System.out.println("a: " + a);  
  9.     }  
  10.     // Overload check for two integer parameters.  
  11.     void check(int a, int b) {  
  12.         System.out.println("a and b: " + a + " " + b);  
  13.     }  
  14.     // overload check for a double parameter  
  15.     double check(double a) {  
  16.         System.out.println("double a: " + a);  
  17.         return a * a;  
  18.     }  
  19. }  
Now we create a main class Overload, which prints all overloaded methods.
  1. class Overload {  
  2.     public static void main(String args[]) {  
  3.         Ovrld ob = new Ovrld();  
  4.         double result;  
  5.         // call all versions of check()  
  6.         ob.check();  
  7.         ob.check(10);  
  8.         ob.check(1020);  
  9.         result = ob.check(123.2);  
  10.         System.out.println("Result of ob.check(123.2): " + result);  
  11.     }  
  12. }  
Output
 
overload1.jpg
 


Similar Articles