Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaInvalid Method Overloading in Java

Invalid Method Overloading in Java

Overloaded methods are those which belong to the same class, having the same name but different arguments. The concept of method overloading came from polymorphism. Literally “poly” means many and “morphism’ means form. 

Consider a real-life example of water as polymorphic because it can take many forms (Solid, Liquid, and gas). Similarly, in java, we can create multiple methods of the same name in the same class. Here we will be discussing the invalid method overloading in java but before that let us revisit method overloading briefly.

Conditions for method overloading

  • We can create multiple methods of the same name in the same class.
  • The number of arguments, sequence of arguments, and types of arguments should differ.

Illustration: Method Overloading 

Java




// Java Program to Illustrate Method Overloading
 
// Main class
class GFG {
 
    // Method 1
    void show()
    {
        // Print statement
        System.out.println("Method to be overloaded.");
    }
 
    // Method 2
    // Overloading Method 1
    // by changing arguments
    void show(int x)
    {
        // Print statement
        System.out.println("Overloaded method:: 1");
    }
 
    // Method 3
    // Overloading show method by changing arguments
    void show(String a, int x)
    {
        System.out.println("Overloaded method:: " + x);
    }
 
    // Method 4
    // Overloading show method
    // by changing arguments
    void show(String a, int b, boolean c)
    {
        System.out.println("Overloaded method:: " + b);
    }
 
    // Method 5
    // Overloading Method 1 by
    // changing arguments as well as return type
    String show(String s)
    {
        // Print statement
        return "Overloaded method:: 5";
    }
 
    // Method 6
    // Main driver method
    public static void main(String[] args)
    {
        // Creating object of class inside main()
        GFG obj = new GFG();
 
        // Calling all methods as defined above
        // to seek overloading concepts
        obj.show();
        obj.show(1);
        obj.show("String", 2);
        obj.show("String", 3, true);
        System.out.println(obj.show("String"));
        obj.show('a');
    }
}


Output-

Method to be overloaded.
Overloaded method:: 1
Overloaded method:: 2
Overloaded method:: 3
Overloaded method:: 5
Overloaded method:: 1

When do the invalid method overloading cases arise?

Invalid method overloading cases arise due to the following reason:

  1. If we try to call more than one method with the same name and argument list. This can be justified from code block 1.
  2. When we try to overload the method by changing return type only. This can be justified from code block 2.

Implementation:

Consider the example given below. When we will try to call “add(1,2)” method the compiler will get confused as there is no such instruction to prefer int over double and vice versa, as a result, it will show a compilation error.

int add(int a, int b)
double add(int a, int b)

Example 1-A

Java




// Demo class
class Demo {
    // Programmer defined "mymethod"
    public int myMethod(int num1, int num2)
    {
        System.out.println("First myMethod of class Demo");
        return num1 + num2;
    }
    // Trying to overload "mymethod"
    public int myMethod(int num3, int num4)
    {
        System.out.println("Second myMethod of class Demo");
        return num4 - num3;
    }
}
// Driver class
class GFG {
    // main method
    public static void main(String args[])
    {
        Demo obj1 = new Demo();
        obj1.myMethod(1, 2);
        obj1.myMethod(3, 4);
    }
}


Output:

prog.java:7: error: method myMethod(int,int) is already defined in class Demo
    public int myMethod(int num3, int num4)
               ^
1 error

Example 1-B

Java




// Java Program to Illustrate No Roleplay of Returntype
// Even changed in Method Overloading
 
// Main class
class GFG {
 
    int a, b;
 
    // Declared method
    void add(int x, int y)
    {
        // This refers to current instance itself
        this.a = x;
        this.b = y;
 
        // Printing the sum
        System.out.println("SUM:: " + (a + b));
    }
 
    // Method 2
    // To add numbers
    // Overloading the above declared method by
    // changing the return type only
    double add(int x, int y)
    {
        this.a = x;
        this.b = y;
        return a + b;
    }
 
    // Method 3
    // Main method method
    public static void main(String[] args)
    {
        // Creating object of class inside main()
        GFG obj = new GFG();
 
        // Calling add() method by passing
        // custom inputs as parameters
        obj.add(5, 2);
 
        // Trying printing the sum
        System.out.println("Sum:: " + obj.add(3, 4));
    }
}


Output:

prog.java:8: error: method add(int,int) is already defined in class GFG
  double add(int x,int y){
         ^
prog.java:17: error: 'void' type not allowed here
      System.out.println("Sum:: "+obj.add(3,4));
                                         ^
2 errors

Conclusion:  We can not call more than one method with the same name and argument list. Return type of method will not play any roles in method overloading, in java it is not possible to achieve overloading by changing the return type of the method only.

RELATED ARTICLES

Most Popular

Recent Comments