Method Overloading with examples in Java

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

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

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

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

Result

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

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

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.

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

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

Here to call the integer version of max() long arguments are explicitly type cast into int.

similar 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 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

In the above program, we have overloaded multiply(…) with a static and nonstatic method.

Output

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

Here method

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

are overloaded.

Output

Can we overload the 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 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

Output

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.

Can we overload static methods in java?

Yes, we can also overload the final method in java.

Q Can We Overload Java Constructors?

Yes, Java constructors can be overloaded.

Q Can we overload the main method in Java?

Yes, we can also overload the main method in java.

Q Can we overload the private method in java?

Yes, we can also overload the private method in java.

Q Can we overload final methods in java?

Yes, we can also overload the final method in java.

Q what is method overloading 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

Q Is method overloading is polymorphism

Yes method overloading is an example of compile-time polymorphism.