In Java programming, Defining two or more methods with 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 method return type may be different.
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 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 method overloading required?
We name methods according to their work.
So there may be situation that same work is performed for different argument list .
To perform same task for different type, order and number of parameters method overloading requires.
as above to add two numbers method add
is created.
Methods with the same name are known as an overloaded method and this process is called method overloading.
How to overload method in java?
Following is the syntax to overload 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 methods return type may differ
- Overloaded methods parameters must be differ by type order and number
- Overloaded methods can throw different exceptions
Method overloading program 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 above class we created three methods with same name to add integers, double and String.
Result
1 2 3 | sum is 9 sum is 9.0 sum is Hello Hi |
How method overloading Works?
When there is a overloaded method a method call is done based on number and type of arguments.
c.add(4,5)
Here both arguments are integer 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 task.
So Method overloading is example of compile time poly morphism.
Type Casting in Method Overloading
Type casting is conversion of one data type to another.
In java method call type casting is also possible
Type promotion in 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 above program we are passing integer arguments in first add. There is no add(int,int) in program.
In this case both arguments are converted to long.
So there is a automatic promotion(widening) is done during 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 first method call c.add(4,5) second argument is converted to long.
In second argument c.add(4,5, 6) first argument is converted to long and third argument is converted to double.
1 2 | long sum is 9 double sum is 15.0 |
There may be chance we have to narrow our data type.
In case of narrowing user has to implicit 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 integer version of max() long arguments are explicitly type casted in to int.
simiar 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 few questions related to overloading lets 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 above program we have overloaded multiply(...)
with static and non static method.
Output
1 2 3 | Integer multiplication is 6 Double multiplication is 6.0 Float multiplication is 6.0 |
You can find inbuilt static overloaded method in Math Class
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)
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 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 main method in Java?
Yes we can also overload main method in java.
Although there may be many main overloaded methods, but 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 many form.
In method overloading a single name is used for overloaded work.
so name is same and form is different.
Overloaded method is resolved by compiler so this is a compile time polymorphism.
Can we overload Java Constructors ?
Yes Java constructors can be overloaded.