Method Overloading in Java

In Java you can have two or more methods having the same name with in the same class provided their arguments differ in either type or number. These types of methods are called overloaded methods and the process is known as method overloading in Java.

Method overloading in Java – What qualifies as overloaded method

1- Overloaded methods in Java can have different number of parameters.
For example

2- Overloaded methods in Java can also differ by the type of parameters.
For example

3- Even if the number of parameters are same as well as type but how they appear differs then also methods are considered to be overloaded.
For Example

4- In case child class has a method having the same name as in parent class but the parameters differ then also it is method overloading in Java. Note that if methods have the same name and same number/types of parameters in both parent and child class then it called method overriding in Java.

Method overloading in Java – What doesn’t qualify as method overloading

If methods in the class differ only by return type then the methods are not considered as overloaded methods.

For example-

In these two add methods parameters are same in count as well as type, only return type differs so these are not considered as overloaded methods.

Calling the correct overloaded method

Static or compile time polymorphism in Java is supported through Method overloading. A call to an overloaded method is bound to the correct method at the compile time itself using the type and/or number of arguments.

Method overloading in Java – Example when number of arguments differ

Output

Method overloading in Java – Example when types of arguments differ

Output

Method overloading in Java – Inheritance example

With inheritance if parent class and child class both have a method having the same name but arguments are different then also the methods are considered to be overloaded.

Output

Here with the object of type B when the add method with two arguments is called the call is to the parent class add method. When the add method with three arguments is called the call is to the child class add method.

Method overloading in Java and automatic type promotion

When an overloaded method is called Java tries to find the method with the exact matching parameters both count and type wise. In case exact match is not found Java tries to resolve the method call by promoting the data types of the method and by looking for a method with higher data type. In automatic type promotion type is promoted to the next highest type and then to next highest type if method call is still not resolved. Order for automatic numeric type promotion is as follows-
byte – short – int – long- float – double

Let’s try to clear it with an example.

Output

Here we have three overloaded add() methods, two of them have three parameters one having float type parameters and another having parameters of type double. There is a call to add() method with 3 int parameters, since there is no add method having 3 int parameters so the add method having three float parameters is called after automatically promoting int to float.

Since float comes before double in type hierarchy so method having float parameters is called.

That’s all for the topic Method Overloading in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

  • Java Pass by Value or Pass by Reference
  • final Vs finally Vs finalize in Java
  • ListIterator in Java
  • wait(), notify() And notifyAll() Methods in Java
  • Java Program to Check if Armstrong Number
  • How to dual-boot Ubuntu and Windows
  • Leave a Reply

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

    This site uses Akismet to reduce spam. Learn how your comment data is processed.