Can two Java methods have the same name with different return type? The return type of the methods are different and they are declared with the same method's name. Show Is that allowed?
Sam R. 15.8k11 gold badges67 silver badges120 bronze badges asked Apr 6, 2011 at 4:25
7 If both methods have same parameter types, but different return type than it is not possible. From Java Language Specification, Java SE 8 Edition, §8.4.2. Method Signature:
If both methods has different parameter types (so, they have different signature), then it is possible. It is called overloading. answered Apr 6, 2011 at 4:29
utharkuthark 5,3032 gold badges44 silver badges59 bronze badges 2 Only, if they accept different parameters. If there are no parameters, then you must have different names.
answered Apr 6, 2011 at 4:28
KaivosukeltajaKaivosukeltaja 15.4k4 gold badges40 silver badges70 bronze badges 3 According the JLS, you cannot however due to a feature/bug in the Java 6/7 compiler (Oracle's JDK, OpenJDK, IBM's JDK) you can have different return types for the same method signature if you use generics.
Prints
For more details, read my article here answered Apr 6, 2011 at 6:28
Peter LawreyPeter Lawrey 520k75 gold badges740 silver badges1118 bronze badges 2 No. C++ and Java both disallow overloading on a functions's return type. The reason is that overloading on return-type can be confusing (it can be hard for developers to predict which overload will be called). In fact, there are those who argue that any overloading can be confusing in this respect and recommend against it, but even those who favor overloading seem to agree that this particular form is too confusing. answered Apr 6, 2011 at 4:28
2 You can have two methods with the same arguments and different return types only if one of the methods is inherited and the return types are compatible. For example:
answered Apr 6, 2011 at 5:13
TofuBeerTofuBeer 60.2k18 gold badges115 silver badges161 bronze badges 1 The java documentation states:
See: http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html answered Jul 17, 2014 at 9:01
ChrisChris 4,5651 gold badge32 silver badges37 bronze badges Even if it is an old thread, maybe some is interested. If it is an option to you to use the same method inside the same class and archive different return types, use generics: Oracle Lesson Generics Simple example for generic value holder class:
And use it like this:
... will result in the following output:
answered Jul 8, 2015 at 12:06
ChristophSChristophS 5684 silver badges23 bronze badges Only if their parameter declarations are different from memory.
answered Apr 6, 2011 at 4:27
ChrisChris 7,74510 gold badges64 silver badges96 bronze badges If it's in the same class with the equal number of parameters with the same types and order, then it is not possible for example:
if the number of parameters and their types is same but order is different then it is possible since it results in method overloading. It means if the method signature is same which includes method name with number of parameters and their types and the order they are defined.
Sam R. 15.8k11 gold badges67 silver badges120 bronze badges answered Nov 13, 2013 at 18:16
unknownunknown 3705 silver badges17 bronze badges Can I have two methods with 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.
What is it called when two methods have the same name but different parameters?Method overloading means two or more methods have the same name but have different parameter lists: either a different number of parameters or different types of parameters.
Which of the following is a method having same name?Which of the following is a method having same name as that of it's class? Explanation: A constructor is a method that initializes an object immediately upon creation. It has the same name as that of class in which it resides.
Which method has the same name as that of its class *?Constructor is a special method that is invoked automatically at the time of object creation. It is used to initialize the data members of new objects generally. Constructor in C++ has the same name as class or structure. Hence the correct answer is Constructor.
|