Java Overloading | Method overloading | Examples

Java Overloading is defining two or more methods with the same name. But Method overloading in Java has the same name with a different number of parameters, different types of parameters, or both. 

Methods overloading comes under the Polymorphism OOPs Concept. And it’s also called compile-time (or static) polymorphism.

Java Overloading, Method overloading Examples constructor rules


The syntax of changing the number of arguments/parameters in method overloading.

Java Method overloading Syntax by changing data types.

Different ways to overload the method

  • Changing the number of arguments/parameters
  • Changing the data type

Note: To achieve methods overloading in java can also do by changing both – parameters and data types.

Example of Java method overloading

When we talking about Java Overdoing then it means a Method overloading in Java. Here we are writing a program of java method overloading example.

Method Overloading with changing of arguments

Let’s make methods of adding numbers (Integer value), there will 2 methods with the same name. One will add 2 numbers and another one will add 3 numbers. So passing data with different numbers of arguments auto detect by java compiler.

For example, defining a static method because the main method is static and it can’t access non-static methods.

Output: 18

Method Overloading: changing the data type of Arguments

The above example was only based on changing the number of Arguments (parameters), now look at how to do overloading in methods by changing a data type in Java.

In the example we have to 2 methods first is with integer and the second is for double numbers adding methods.

Output: 18


Constructor Overloading

You can do Constructor overloading in Java, in the same ways as used in methods overloading. The overloaded constructor is called or selected based on when creating an object used the new keyword specified.

See below Constructor overloading example.

Output: Default Constructor
Constructor Overloading

Java overloading operators

Java does not allow operator overloading. The preferred approach is to define a method in your class to perform the action: a.add(b) instead of a + b.

Check Method overloading rules in java :

  • All method’s name should be the same.
  • If argument/parameters overloading then parameters should be different.
  • Only with different return types of methods can’t achieve methods overloading.

Q: Why Method Overloading is not possible by only changing method the return type?

Answers: Only changing a return data type in Java method overloading is not possible because of data ambiguity.

Let’s try an example of Overloading with the same methods name, arguments but different return types. It will definitely show a Compile Time Error.

Screenshot of error: Error:(6, 19) java: method add(int,int) is already defined in class MethodOverloading

data ambiguity methods overloading in java error

Q: Can we overload java’s main() method? [ Very Important Question)

Answers: Yes it is possible as many times you want can do overloading on Java Main() Methods.

Now Which one main method will call?

JVM will only call a main() method which has string array as arguments only. Look at the below example it.

Output: main with String[] Array

Q: Why method overloading?

Answers: Some uses and advantages of Methods overloading in java. Easy to remember – Not need to create multiple methods with different to do the same operation.

Or in the case, if your methods are updated for new Application, that time you can create it with the same name. The old methods support older version of the application and a new one for new application under the same name. (Methods Version)

Sidenote: Because of Overloading is using on methods that’s why it’s also called Method overloading in Java.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1

Java version 11

All Java Overloading Methods Examples are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.