Dive into Java Method Overloading

Method overloading allows a class to have two or more methods having same name. But there are some conditions applied if you overload a method. Let’s discuss on then.

First of all we should know “ Why do we use method overloading in Java?” .

Suppose we have to perform addition of given number but there can be any number of arguments, if we write method such as methodAddTwoNo(int a, int b) for two arguments, methodAddThreeNo(int a, int b, int c) for three arguments then it is very difficult for you and other programmer to understand purpose or behaviors of method they can not identify purpose of method. So we use method overloading to easily figure out the program. For example above two methods we can write sum(int a, int b) and sum(int a, int b, int c) using method overloading concept.

So it is clear that to achieve method overloading concept methods should have same name but with different parameters.

But what will happen if we put methods name same and also the same parameters.

Let’s see:

public class OverloadJava
{
public static void main(String[] args){
sum();
}

void sum(){
}

void sum(){
}
}

In the above class two methods with same name and no input parameter. This will be a compile time error because these methods are duplicate.

Now if we change the return type of one method, what will happen???

Still the same because JVM is still in confusion that which method​ is being called. So again it’s a compile time error.

Now the question is “Can we overload main method?”

Yes we can overload main method because main method is static and it follows all rules for static keyword in java, so you can say that we can overload a static method in java.

public class OverloadJava
{
public static void main(String[] args){
System.out.println(“main”);
}

public static void main(){
System.out.println(“main 2”);
}

}

Now you will be thinking can we overload a final method??

Answer is Yes, we can overload a final method but remember input parameters should be different.

So the conclusion is “One and only rule of method overloading in Java is that method signature of all overloaded method must be different. Method signature is changed by changing either number of method arguments, or type of method arguments e.g. System.out.println() method is overloaded to accept different primitive types like int, short, byte, float etc. They all accept just one argument but their type is different. You can also change method signature by changing order of method argument.”

@source https://medium.com/@shandilya.aman/dive-into-java-method-overloading-afc22bcf9678

Advertisements

5 thoughts on “Dive into Java Method Overloading

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s