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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package com.infotech.client; /** * @author KK JavaTutorials * Method overloading in Java */ public class MethodOverloadingTest { // Method with 2 int parameter2 void myOverloadedMethod(int data1,int data2) { System.out.println("Inside myOverloadedMethod with 2 int parameters " + data1+" "+data2); } // Method with 3 int parameters void myOverloadedMethod(int data1, int data2,int data3) { System.out.println("Inside myOverloadedMethod with 3 int parameters " + data1 + " " + data1+" "+data3); } // Method with 2 float parameters void myOverloadedMethod(float number1,float number2) { System.out.println("Inside myOverloadedMethod with 2 float parameter " + number1+" "+number2); } public static void main(String args[]) { MethodOverloadingTest methodOverloadingTest = new MethodOverloadingTest(); methodOverloadingTest.myOverloadedMethod(40,50); methodOverloadingTest.myOverloadedMethod(80, 90,100); methodOverloadingTest.myOverloadedMethod(30.8f,20.5f); } } |
Output:
1 2 3 |
Inside myOverloadedMethod with 2 int parameters 40 50 Inside myOverloadedMethod with 3 int parameters 80 80 100 Inside myOverloadedMethod with 2 float parameter 30.8 20.5 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.infotech.client; /** * @author KK JavaTutorials * Method overloading in Java and automatic type conversion example */ public class OverloadingTest2 { //Method overloaded with 1 long parameter void myOverloadedMethod(long i) { System.out.println("myOverloadedMethod with one long parameter " + i); } //Method overloaded with 2 float parameters void myOverloadedMethod(float num1, float num2) { System.out.print("myOverloadedMethod with 2 float parameters " + num1 + " " + num2+"\n"); } public static void main(String args[]) { OverloadingTest2 overloadingTest2 = new OverloadingTest2(); overloadingTest2.myOverloadedMethod(90); overloadingTest2.myOverloadedMethod(40.7f, 200.30f); overloadingTest2.myOverloadedMethod(59, 90); } } |
Output:
1 2 3 |
myOverloadedMethod with one long parameter 90 myOverloadedMethod with 2 float parameters 40.7 200.3 myOverloadedMethod with 2 float parameters 59.0 90.0 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.infotech.client; public class MyParentClass { private int number; public MyParentClass(int number) { this.number = number; } // Method with no parameter public void dispay() { System.out.println("Number Value from ParentClass class: " + number); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.infotech.client; public class MyChildClass extends MyParentClass { private int j; public MyChildClass(int i, int j) { //Calling parent class constructor super(i); this.j = j; } // Overloaded Method with String parameter public void dispay(String message) { System.out.println(message); System.out.println("Value of j: " + j); } } |
1 2 3 4 5 6 7 8 9 10 11 |
package com.infotech.client; public class ClientTest { public static void main(String args[]) { MyChildClass child = new MyChildClass(50, 60); // This call will invoke the child class method child.dispay("Calling ChildClass class display"); // This call will invoke the parent class method child.dispay(); } } |
Output of ClientTest.java:
1 2 3 |
Calling ChildClass class display Value of j: 60 Number Value from ParentClass class: 50 |
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
You May Also Like:
Encapsulation in Java
Abstraction in Java
Method Overriding or Dynamic Polymorphism in Java
Understating of Inheritance in Java
Difference Between Encapsulation and Abstraction in Java
Association Aggregation And Composition in Java
If you have any feedback or suggestion please feel free to drop in the blow comment box.