Skip to content

Java Overloading | Method overloading | Examples

  • by

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

Syntax

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

int add_method(int, int)
int add_method(int, int, int)

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 the java compiler.

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

public class MethodOverloading {
    static int add(int a, int b) {
        return a + b;
    }

    static int add(int a, int b, int c) {
        return a + b + c;
    }
    public static void main(String[] args) {
        System.out.println(MethodOverloading.add(2, 16));
        System.out.println(MethodOverloading.add(78, 1, 9));
    }
}

Output: 18
88

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.

public class MethodOverloading {
    static int add(int a, int b) {
        return a + b;
    }

    static double add(double a, double b) {
        return a + b;
    }
    public static void main(String[] args) {
        System.out.println(MethodOverloading.add(2, 16));
        System.out.println(MethodOverloading.add(78.9, 1.9));
    }
}

Output: 18

80.80000000000001

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.

public class ConstructorOverloading {

    //default Constructor
    public ConstructorOverloading() {
        System.out.println("Default Constructor");
    }

    //Constructor Overloading
    public ConstructorOverloading(String r) {
        System.out.println(r);
    }

    //Constructor Overloading
    public ConstructorOverloading(int r1, int r2) {
        System.out.println(r1 + r2);
    }

    public static void main(String arg[]) {
        ConstructorOverloading co = new ConstructorOverloading();

        ConstructorOverloading co1 = new ConstructorOverloading("Constructor Overloading");

        ConstructorOverloading co2 = new ConstructorOverloading(1, 6);
    }
}

Output: Default Constructor
Constructor Overloading
7

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 names 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 method name, arguments but different return types. It will definitely show a Compile Time Error.

public class MethodOverloading {
    static int add(int a, int b) {
        return a + b;
    }

    static double add(int a, int b) {
        return a + b;
    }
    public static void main(String[] args) {
        System.out.println(MethodOverloading.add(2, 16));
        System.out.println(MethodOverloading.add(78, 1));
    }
}

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.

public class MethodOverloading {
    public static void main(String[] args) {
        System.out.println("main with String[] Array");
    }

    public static void main(String args) {
        System.out.println("Overloading main with String");
    }

    public static void main() {
        System.out.println("Overloading main without args");
    }
}

Output: main with String[] Array

Q: Why method overloading?

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

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

Side note: 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

Your email address will not be published. Required fields are marked *