Forums Register Login

Why return type is not sufficient for method overloading in java ?

+Pie Number of slices to send: Send
 


Example:

if we write :

int test();

String test();

call :

String str = "prabhat" + test();



Result is Duplicate method at compile time only !
+Pie Number of slices to send: Send
I guess the short answer is... that is how it is defined in the Java language specification. Section 8.4.9, to be exact.

Unless, of course, the OP is asking why it was specified that way, which I don't know.

Henry
+Pie Number of slices to send: Send
class Dog extends Animal {};
class Cat extends Animal {};

Dog test();

Cat test();

Animal A = test();

Which version of test should the JVM run?
+Pie Number of slices to send: Send
 

fred rosenberger wrote:
Which version of test should the JVM run?



I agree with Fred, in that it is certainly easy to confuse the compiler, if return types were involved. But it is also possible to confuse the compiler, even if return types are not involved. This is why the JLS added the qualifier that the parameters be not "override-equivalent".

But... if I had to take a guess why, I will say that it is because no other languages define overloading this way (return type as part of the signature) -- certainly not the C++ language which Java was modelled after.

Henry
I got this tall by not having enough crisco in my diet as a kid. This ad looks like it had plenty of shortening:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1028 times.
Similar Threads
String literals
Help.explanation of code from different classes
overloaded method
If statement containing casting causes error
Some Codes
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:32:49.