Method overloading in java

If two or more methods have same name , but different argument then it is called method overloading.

Why you would do that (same name but different argument)?

Lets take an example. You want to print salary of employee and sometimes company gives bonus to their employee and sometimes it don’t.So If company don’t give bonus then we can use printSalary(int salary) method and if it provides bonus then we can use printSalary(int salary,int bonus) so both methods are doing same work but their inputs are different so it will increase readability of programs.Otherwise if you give different methods name,it will become hard to understand.
When you run above program, you will get following output:

Salary without bonus : 20000
**********************
Salary with bonus : 30000

Rules of Method overloading :

Number of Arguments Overloaded method can have different number of arguments
Date type Overload method can have different data type for argument
Return type Return type can be changed but either number of argument or data type of argument should also be changed..
Order of arguments If you change sequence of arguments then it is also valid method overloading provided you have different data types arguments.
Constructor Can be overloaded

So you can overload method using three ways:

  1. By changing number of arguments
  2. By changing data type of arguments
  3. By changing sequence of arguments if they are of different types

By changing number of arguments

Above example which we have taken is of this type.We are overloading printSalary() method with different number of argument.

By changing data type of arguments:

In above example, we will create another method, which will take double data type as input.

 so here we have introduced a new method which takes double datatype as input.This is also valid method overloading.

By changing sequence of argument if they are of different data types

We can introduce a new method printSalary(double bonus,int salary). so by changing order of argument we can overload method.

Why we can’t change only return type?

 If we change only return type, it will become ambiguous for compiler to figure out which method to call.That is why you can not change only return type.

What is static binding?

When you compile Java program. During compilation process, compiler bind method call to actual method. This is called static binding and method overloading binding happens at compile time.

Related Posts

  • 30 January

    Difference between early binding and late binding in java

    Binding in Java refers to the process of associating a method or function body with a method or function call by the Java Compiler. In simpler words, when a function is called by the Java compiler the task of correctly locating the method for the respective function is binding. Depending on when the compiler is […]

  • 04 November

    Can we overload main method in java

    In this post, we will see about "Can we overload main method in java".This is one of the most asked Core java interview questions. Yes, we can overload main method in java but when you run your program, JVM will search for public static void main(String[] args) and execute that method. Overload main method in […]

  • 02 November

    Dynamic method dispatch in java

    In this post, we will about Dynamic method dispatch which is also referred as run time polymorphism. Dynamic Method Dispatch Dynamic method dispatch is a technique by which call to a overridden method is resolved at runtime, rather than compile time.When an overridden method is called by a reference, then which version of overridden method […]

  • 29 October

    Can we override static method in java

    No, we can not override static method in java. Static methods are those which can be called without creating object of class,they are class level methods. On other hand,If subclass is having same method signature as base class then it is known as method overriding. Its execution decided at run time. Below are the reasons why […]

  • 26 August

    Copy Constructor in java

    In this post, we will see about copy constructor in java. Copy constructor is the constructor which takes parameter as object of same class and copy each field of the class to the new object. Unlike C++, java does not provide default copy contructor. You need to create one, if you want to have copy […]

  • 26 August

    Java default constructor

    In this post, we will see about Java default constructor. Default constructor is the no arg constructor which is inserted by compiler unless you provide any other constructor explicitly. You won’t able to see it as it is present in class file rather than source file. Is there any difference between no argument constructor and […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.