what is method overloading in java?
In Java programming, Defining two or more methods with the same name with different parameter list are called Method Overloading in Java.
Like
void add(int a,int b);
void add(double a, double b);
void add(String a,String b);
int add(int a,int b,int c);
In method overloading, the method return type may be different.
The above methods are overloaded.
Java inbuilt overloaded methods
There are many overloaded methods available in java.
You already used many of them.
To print on the console, we use System.out.println()
method println has many overloaded methods
These are
1 2 3 4 5 6 7 8 9 10 | void println() void println(boolean x) void println(char x) void println(char[] x) void println(double x) void println(float x) void println(int x) void println(long x) void println(Object x) void println(String x) |
You can also find overloaded method for println()
and print()
here
Why is method overloading required?
We name methods according to their work.
So there may be a situation the same work is performed for different argument list.
To perform the same task for different types, orders and a number of parameters method overloading require.
as above to add two numbers method add
is created.
Methods with the same name are known as an overloaded methods and this process is called method overloading.
How to overload a method in java?
Following is the syntax to overload the method
1 2 3 4 5 6 7 8 9 10 11 | class className { <accessSpecifier> <returnType> methodName() { // overloaded method } <accessSpecifier> <returnType> methodName(parameter1) { // overloaded method } <accessSpecifier> <returnType> methodName(parameter1, parameter2) { // overloaded method } <accessSpecifier> <returnType> methodName(parameter1, parameter2, parameter3) {// overloaded method } } |
- Overloaded access specifier may differ
- Overloaded method’s return types may differ
- Overloaded methods parameters must differ by type order and number
- Overloaded methods can throw different exceptions
method overriding example in java
Method overloading example in java to add two numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public class MyClass { void add(int a, int b) { int sum = a + b; System.out.println("sum is " + sum); } void add(double a, double b) { double sum = a + b; System.out.println("sum is " + sum); } void add(String a, String b) { String sum = a + b; System.out.println("sum is " + sum); } public static void main(String args[]) { MyClass c = new MyClass(); c.add(4, 5); c.add(4.0, 5.0); c.add("Hello", " Hi"); } } |
In the above class, we created three methods with the same name to add integers, doubles and String.
Result
1 2 3 | sum is 9 sum is 9.0 sum is Hello Hi |
How does method overloading Work?
When there is an overloaded method a method call is done based on the number and type of arguments.
c.add(4,5)
Here both arguments are integers so add(int, int) is get called.
similar, c.add(4.0,5.0)
here both arguments are double so add(double, double) gets called.
Resolving which method is getting called is done at compile time.
Here one name is performing many tasks.
So Method overloading is an example of compile time polymorphism.
Type Casting in Method Overloading
Type casting is the conversion of one data type to another.
In the java method call type casting is also possible
Type promotion in a java method call is as below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class MyClass { void add(long a, long b) { long sum = a + b; System.out.println("long sum is " + sum); } void add(double a, double b) { double sum = a + b; System.out.println("double sum is " + sum); } public static void main(String args[]) { MyClass c = new MyClass(); c.add(4, 5); c.add(4.0f, 5.0f); } } |
1 2 | long sum is 9 double sum is 9.0 |
In the above program, we are passing integer arguments in the first add. There is no add(int, int) in the program.
In this case, both arguments are converted to long.
So there is an automatic promotion(widening) is done during a method call.
similar for c.add(4.0f, 5.0f)
.
Lets see again another example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class MyClass { void add(int a, long b) { long sum = a + b; System.out.println("long sum is " + sum); } void add(long a, int b, double c) { double sum = a + b + c; System.out.println("double sum is " + sum); } public static void main(String args[]) { MyClass c = new MyClass(); c.add(4, 5); c.add(4, 5, 6); } } |
In the first method call c.add(4,5) the second argument is converted to long.
In the second argument c.add(4,5, 6) the first argument is converted to long and the third argument is converted to double.
1 2 | long sum is 9 double sum is 15.0 |
There may be a chance we have to narrow our data type.
In case of narrowing user has to implicitly cast the parameter.
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 30 31 32 | class Relation{ public int max(int i,int j){ if(i>j){ return i; }else{ return j; } } public float max(float f,float j){ if(f>j){ return f; }else{ return j; } } } public class MyClass { public static void main(String args[]) { long a=3,b=4; double d=4.3,e=4.4; Relation r=new Relation(); int max=r.max((int)a,(int)b); System.out.println("Max is " + max); float max1=r.max((float)d,(float)e); System.out.println("Max is " + max1); } } |
Here to call the integer version of max() long arguments are explicitly type cast into int.
similar for double values.
Output
1 2 | Max is 4 Max is 4.4 |
Method Overloading in child class
Method overloading can also be used in inheritance.
A method can also be overload in child class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | class Parent{ void add(long a,long b){ long sum=a+b; System.out.println("Adding to long in Parent"); System.out.println("Addition is "+sum); } } class Child extends Parent{ void add(double a,double b){ double sum=a+b; System.out.println("Adding to double in Child"); System.out.println("Addition is "+sum); } } public class MyClass { public static void main(String args[]) { Child child=new Child(); child.add(12,12); child.add(5.0,4.0); } } |
Here add method is overloaded in inheritance hierarchy.
1 2 3 4 | Adding to long in Parent Addition is 24 Adding to double in Child Addition is 9.0 |
There are a few questions related to overloading let’s see them.
Can we overload static methods in java?
Yes We can overload static methods in java as below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public class Overload1{ static void multiply(int x,int y){ int mul=x*y; System.out.println("Integer multiplication is "+mul); } static void multiply(double x,double y){ double mul=x*y; System.out.println("Double multiplication is "+mul); } void multiply(float x,float y){ float mul=x*y; System.out.println("Float multiplication is "+mul); } public static void main(String args[]) { multiply(2,3); multiply(2.0,3.0); Overload1 o=new Overload1(); o.multiply(2.0f,3.0f); } } |
In the above program, we have overloaded multiply(…) with a static and nonstatic method.
Output
1 2 3 | Integer multiplication is 6 Double multiplication is 6.0 Float multiplication is 6.0 |
You can find an inbuilt static overloaded method in Math Class
the overloaded max method in Math class is as below
static double max(double a, double b)
static float max(float a, float b)
static int max(int a, int b)
static long max(long a, long b)
The overloaded min method in Math class is as below
static double min(double a, double b)
static float min(float a, float b)
static int min(int a, int b)
static long min(long a, long b)
Can we overload final methods in java?
Yes we can overload final methods in java
Lets understand by example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | class Overload2{ final void show(){ System.out.println("Calling final show()"); } final void show(int a ){ System.out.println("Calling final show(int a ) "+a); } void show(int a,int b){ System.out.println("Calling show(int a,int b ) "+a+" "+b); } } public class MyClass { public static void main(String args[]) { Overload2 o=new Overload2(); o.show(); o.show(4); o.show(4,5); } } |
Here method
final void show()
final void show(int a )
void show(int a,int b)
are overloaded.
Output
1 2 3 | Calling final show() Calling final show(int a ) 4 Calling show(int a,int b ) 4 5 |
Can we overload the private method 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 30 31 32 33 | public class Overload3{ private int min(int a,int b){ if(a>b){ return b; }else{ return a; } } private float min(float a,float b ){ if(a>b){ return b; }else{ return a; } } double min(double a,double b){ if(a>b){ return b; }else{ return a; } } public static void main(String args[]) { Overload3 o=new Overload3(); int c=o.min(4,3); System.out.println("Minimum number is "+c); float f=o.min(4.2f,3.2f); System.out.println("Minimum number is "+f); double d=o.min(4.0,5.0); System.out.println("Minimum number is "+d); } } |
In above program
private int min(int a,int b)
private float min(float a,float b )
double min(double a,double b)
are overloaded
Above two private methods can not be accessible out side the class.
Output
1 2 3 | Minimum number is 3 Minimum number is 3.2 Minimum number is 4.0 |
Can we overload the main method in Java?
Yes, we can also overload the main method in java.
Although there may be many main overloaded methods, execution starts from
public static void main(String args[])
following is the example
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 30 31 32 33 34 35 36 | public class Overload3{ public static void main() { System.out.println("Calling public static void main()"); } public static void main(String s) { System.out.println("Calling public static void main(String s)"); } public static void main(int a,int b) { System.out.println("Calling public static void main(int a,int b)"); } void main(long l) { System.out.println("Calling void main(long l)"); } private void main(double l) { System.out.println("Calling private void main(double l)"); } final void main(long d,long e) { System.out.println("Calling final void main(long d,long e)"); } public static void main(String args[]) { System.out.println("Calling public static void main(String args[]) "); /*calling static methods*/ main(); main(2,3); main("Hello"); /*calling Instance method*/ Overload3 o=new Overload3(); o.main(3l); o.main(3.0); o.main(3,4); } } |
Output
1 2 3 4 5 6 7 | Calling public static void main(String args[]) Calling public static void main() Calling public static void main(int a,int b) Calling public static void main(String s) Calling void main(long l) Calling private void main(double l) Calling public static void main(int a,int b) |
Is method overloading is polymorphism
Yes.
Polymorphism is one name of many forms.
In method overloading a single name is used for overloaded work.
so the name is the same and the form is different.
The overloaded method is resolved by the compiler so this is a compile-time polymorphism.
Yes, we can also overload the final method in java.
Yes, Java constructors can be overloaded.
Yes, we can also overload the main method in java.
Yes, we can also overload the private method in java.
Yes, we can also overload the final method in java.
two or more methods with the same name with different parameter lists are called overloaded methods and this process is known as Method Overloading
Yes method overloading is an example of compile-time polymorphism.