Can two methods have the same name but different return type?
Java can distinguish the methods with different method signatures. i.e. the methods can have the same name but with different parameters list (i.e. the number of the parameters, the order of the parameters, and data types of the parameters) within the same class. Show Overloaded methods are differentiated based on the number and type of the parameters passed as an argument to the methods. If we need to do some kind of the operation with different ways i.e. for different inputs. In the example described below, we are doing the addition operation for different inputs. It is hard to find many meaningful names for a single action. Different ways of doing overloading methods Method overloading can be done by changing:
Method 1: By changing the number of parameters. import java.io.*; class Addition { // adding two integer values. int sum = a + b; // adding three integer values. int sum = a + b + c; class P_AI { Addition ob = new Addition(); int sum1 = ob.add(1, 2); Method 2: By changing the Data types of the parameters import java.io.*; class Addition { // adding three integer values. int sum = a + b + c; // adding three double values. double sum = a + b + c; class P_AI { Addition ob = new Addition(); int sum2 = ob.add(1, 2, 3); Method 3: By changing the Order of the parameters import java.io.*; class Prutor { public void prutorIdentity(String name, int id) System.out.println("prutorName :" + name + " " public void prutorIdentity(int id, String name) System.out.println("Id :" + id + " " class P_AI { Prutor prutor = new Prutor(); prutor.prutorIdentity("Rohit", 1); prutorName :Rohit Id :1 What happens when method signature is the same and the return type is different? The compiler will give an error as the return value alone is not sufficient for the compiler to figure out which function it has to call. More
details:Java Language Specification: §8.4.2. // Example to show error when method signature is same class Addition { int sum = a + b; // adding three integer value. class P_AI { int sum1 = ob.add(1, 2); int sum2 = ob.add(1, 2); 16: error: method add(int,int) is already defined in class Addition Method Overloading and Null error in Java Can you have two methods with the same name?Two methods may share the same name, provided the number of parameters are different, or if they both have the same parameters, then there is at least one position, i where the parameter types differ.
Can we have 2 return types in Java?No, you don't have two return types. It's a generic method you are seeing.
Can we overload same method with different return type?No, you cannot overload a method based on different return type but same argument type and number in java.
Can you have multiple methods with the same name in Java?In Java, two or more methods may have the same name if they differ in parameters (different number of parameters, different types of parameters, or both). These methods are called overloaded methods and this feature is called method overloading.
|