Whenever same method name is exiting multiple times in the same class with different number of parameter or different order of parameters or different types of parameters is known as method overloading.   

Advantage of Method Overloading in Java

One significant advantage of method overriding is that a class can give its specific execution to an inherited method without having the modification in the parent class (base class).

Why use the method Overloading in Java?

Suppose we have to perform addition of a given number but there can be any number of arguments, if we write a method such as an (int, int)for two arguments, b(int, int, int) for three arguments then it is very difficult for you and another programmer to understand purpose or behaviors of the method they can not identify the purpose of the method. So we use method overloading to easily figure out the program. For the example above two methods, we can write sum(int, int) and sum(int, int, int) using the method overloading concept.
Whenever same method name is exiting multiple times in the same class with different number of parameter or different order of parameters or different types of parameters is known as method overloading.
 

Different ways to overload the method

There are two ways to overload the method in java

  • By changing number of arguments or parameters
  • By changing the data type

By changing number of arguments

In this example, we have created two overloaded methods, first sum method performs addition of two numbers and second sum method performs addition of three numbers.

Example Method Overloading in Java


Output :

            30
            60

Why Method Overloading is not possible by changing the return type of method?

In java, method overloading is not possible by changing the return type of the method because there may occur ambiguity. Let's see how ambiguity may occur:

because there was problem:

Program:

class Addition { int sum(int a, int b) { System.out.println(a+b); } double sum(int a, int b) { System.out.println(a+b); } public static void main(String args[]) { Addition obj=new Addition(); int result=obj.sum(20,20); //Compile Time Error } }

Output:

int result=obj.sum(20,20);

  • Progamming is what actually means ?
  • What is software development ?
  • How we categorized software development in different manner.
C Programming
C Plus Plus
Python
Java Development
Web Designing
Javascript
Mysql
Oracle
We cover all the programming concepts in various programming languages, this tutorials are very help full for bigener as well as Experience developer, for the ease of understanding we categorized programming in different manner likewise.