Method Overloading in Java

Method Overloading in Java

Two methods are said to be overloaded if and only if both methods have the same name but different argument types.

In the C language method, the overloading concept is not applicable, hence we cannot declare multiple methods with the same name and different argument types. If there is a change in argument type we must go for a new method name, it increases the complexity of programming.

But in Java, we can declare multiple methods with the same name but different argument types, such types of methods are called as overloaded methods.

Having an overloading concept in Java reduces the complexity of the programming.

public class MethodOverloading {
    public void method(){
        System.out.println("No- Argument");
    }
    public void method(int a){
        System.out.println("Method with one argument");

    }
    public void method(int a, int b){
        System.out.println("Method with two argument");

    }

    public static void main(String[] args) {
        MethodOverloading object = new MethodOverloading();
        object.method();
        object.method(2);
        object.method(1,2);
    }
}
method overloading in java

Note- In the overloading method resolution is always taken care of by the Java compiler based on reference type, hence overloading is also considered as compile4 time polymorphism or early binding.

Method Overloading Different Scenario1

Scenario1

Automatic promotion in overloading while resolving overloaded methods, If the exact match method is not available then we would not get any compile time error immediately first compiler will promote the argument to the next level and check whether the matched method is available or not, if the matched method will available then this method will be considered. If the matched method is not available then the compiler promotes the argument once again to the next level. This process will continue untill all possible promotions, still if the matched method is not available then we will get compile time error.

Following are the all possible promotion in overloading.

byte -> short -> int -> long -> float -> double

char -> int -> long -> float -> double

This process is called automatic promotion in overloading.

public class MethodOverloading {
    public void method(int a) {
        System.out.println("int-arg");
    }

    public void method(float f) {
        System.out.println("float-arg");

    }
    public static void main(String[] args) {
        MethodOverloading object = new MethodOverloading();
        object.method('A'); // char will be promoted to int data type
        object.method(5l); // long will be promoted to float argument
        object.method(1); // there is no promotion require
    }
}
Method Overloading in Java

Note- While resolving an overloaded method compiler will always give precedence to the child type argument in comparison with parent type argument.

Scenario2

The var-args method will get the least priority that is if there is no other method matched then only the var-args method will get a chance. It is the same as the default case in the switch statement in Java.

public class MethodOverloading {
    public void method(int a) {
        System.out.println("int-arg");
    }

    public void method(int... x) {
        System.out.println("var-arg");

    }
    public static void main(String[] args) {
        MethodOverloading object = new MethodOverloading();
        object.method();//Var-arg method will be called
        object.method(1); //int argument method will be called
        object.method(1,2); // Var-arg method will be called
    }
}
overloading in Java

Similar Java Tutorials

  1. Bitwise operator in Java
  2. Operators in Java
  3. Public static void main
  4. Command line argument in Java
  5. Java coding standards
  6. Control Flow Statements in Java
  7. If – else in Java
  8. Switch statements in Java
  9. For loop in Java
  10. While loop in Java
  11. Do while loop in Java
  12. Import statement in Java
  13. Abstract modifier in Java
  14. Strictfp modifier in Java
  15. Final variable in Java
  16. Static modifier in Java
  17. Native modifier in Java
  18. Transient keyword in Java
  19. Volatile keyword in Java
  20. Adapter Class in Java
  21. Method signature in Java
  22. Method overloading in Java
  23. Has A Relationship
  24. Inheritance in Java
  25. Encapsulation in Java
  26. Abstraction in Java
  27. Data Hiding in Java
  28. Interface vs abstract class