Method Overloading in Java

In Java programming, Defining two or more methods with same name with different parameter list are called Method Overloading.

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

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

  • 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.

In above class we created three methods with same name to add integers, double and String.

Result

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

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

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.

There may be chance we have to narrow our data type.

In case of narrowing user has to implicit cast the parameter.

Here to call integer version of max() long arguments are explicitly type casted in to int.

simiar for double values.

Output

Method Overloading in child class

Method overloading can also be used in inheritance.

A method can also be overload in child class

Here add method is overloaded in inheritance hierarchy.

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

In above program we have overloaded multiply(...) with static and non static method.

Output

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

Here method

final void show()
final void show(int a )
void show(int a,int b)

are overloaded.

Output

Can we overload private method in java?

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

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

Output

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.