Reader Level:
Articles

Method Overloading In Java

By Sandeep Sharma on April 10, 2013
In this article we will discuss method overloading in Java.
  • 0
  • 0
  • 3121

Introduction

In this article we will discuss about method overloading in Java. We also discuss method overloading sub-parts why we need that, there 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 have 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 method with same name but have different parameters. This methods are said to be overloaded, and the process is referred as method overloading. Lets take an example

  • Define a method check() which contain single parameter.
  • void check(int a)
  • In this example we have define a method check() with single parameter.
    void check(int a, int b)
  • This example contain a method with same name called check() which contains two parameters. Hence both the method 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 of 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 method. 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 different signature. The below points brief what does method signature means in Java
             1.
    Return type of method is not part of method signature in Java. 
             2. Number of argument to a method is part of method signature.
             3.
    Type of argument to a method is also part of method signature
             4. 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. 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. 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

  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).

//method overloading.
class Ovrld
  {
    void check()
      {
        System.out.println("No parameters");
      }
    //
Overload check for single integer parameter
    void
check(int a)
      {
       
System.out.println("a: " + a);
      }
    //
Overload check for two integer parameters.
    void check(int a, int b)
      {
        System.out.println("a and b: " + a + " " + b);
      }
    //
overload check for a double parameter
    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;
        //
call all versions of check()
        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

overload1.jpg

Sandeep Sharma

I am Java Developer working on JAVA and Android Technology. I have 16+ months experience in JAVA Tech. & 6+ months experience in Android Programming.

COMMENT USING

Trending up