Invalid Method Overloading in Java
Last Updated :
30 Sep, 2021
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:
- If we try to call more than one method with the same name and argument list. This can be justified from code block 1.
- 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.
Similar Reads
Method Overloading in Java In Java, Method Overloading allows us to define multiple methods with the same name but different parameters within a class. This difference may include:The number of parametersThe types of parametersThe order of parametersMethod overloading in Java is also known as Compile-time Polymorphism, Static
10 min read
Overloading Variable Arity Method in Java Here we will be discussing the varargs / variable arity method and how we can overload this type of method. So let us first understand what a variable arity method is and its syntax. A variable arity method also called as varargs method, can take a number of variables of the specified type. Note: Un
5 min read
Different Ways of Method Overloading in Java In Java, Method overloading refers to the ability to define multiple methods with the same name but with different parameter lists in the same class. It improves code readability and reusability, Instead of assigning different names to methods performing similar operations, we can use the same name,
5 min read
Overriding equals method in Java Consider the following Java program: Java class Complex { private double re, im; public Complex(double re, double im) { this.re = re; this.im = im; } } // Driver class to test the Complex class public class Main { public static void main(String[] args) { Complex c1 = new Complex(10, 15); Complex c2
3 min read
Method Overloading and Ambiguity in Varargs in Java Prerequisite - Varargs , Method Overloading Method Overloading in Varargs Overloading allows different methods to have same name, but different signatures where signature can differ by number of input parameters or type of input parameters or both. We can overload a method that takes a variable-leng
5 min read