Method Overloading or Static Polymorphism in Java

By | September 25, 2018

In this of Method Overloading or Static Polymorphism in Java  you will understated about method Overloading in java using various example.

When two or more methods with in the same class or within the parent-child relationship classes, have the same name but the parameters are different in types or number, the methods are said to be overloaded. This kind  of overloaded methods is known as method overloading in Java.

Method overloading is one of the ways through which java supports Static Polymorphism.

When an overloaded method is called, which method is to be called is determined through –

  • Types of the parameters.
  • Number of the parameters.

Please note that return type of the methods may be different but that alone is not sufficient to determine the method that has to be called.

Method overloading Example in Java

Output:

Here we have 3 methods with the same name but parameters are different either in number or type. first method has 2 int parameter2, second method has 3 int parameters and yet third method has 2 float parameter2.

Note: If you look into below classes within JDK You will  find these classes having lot of overloaded methods(given are the few classes)

  • java.lang.Math
  • java.util.Collections
  • java.lang.String
  • java.lang.Integer
  • java.lang.Long
  • java.lang.Double etc………

Method overloading in Java and automatic type conversion

In java automatic type promotion may happen and it does have a role in how overloaded methods are called, let’s see it with an example to have more clarity.

Output:

Here you notice the third call to the method
overloadingTest2.myOverloadedMethod(59, 90);

It has 2 int parameters, but there is no overloaded method with 2 int parameters, in this case automatic type conversion happens and java promotes these 2 int parameters to float and calls myOverloadedMethod(float num1, float num2) instead.

Overloaded method in case of Inheritance

In the case of parent-child relationship i.e. inheritance if both classes have the method with same name and same number and type of parameters then it is considered as method overriding, otherwise it is method overloading.

Output of ClientTest.java:

Benefit of Method Overloading

If we do not have method overloading then the methods which have same functionality but parameters type or number of parameters differs should have different names, thus it will reduce readability of the program.

As example – If yo have a method to add two numbers but types may differ then there will be different methods for different types like addInt(int a, int b)addLong(float a, float b) and so on without overloaded methods.

With method overloading we can have the same name for the overloaded methods thus increasing the readability of the program.

That’s all about Method Overloading in Java
If you have any feedback or suggestion please feel free to drop in blow comment box.

Leave a Reply

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