What you are actually trying to do is overriding a method. Overloading is a method in the same class with a different set of arguments. When you override, you must have the same method signature, return type too. Imagine the following code:
In the case of overloading how could the compiler identify which function needs to be invoked (in case the parameter lists are not different like the cases Ludwig mentioned)? The two functions mentioned in the sample code has same arguments(none) so the compiler will not be able to figure out which one of the two you intend to invoke?
Since you read my reply to the original question, what is your take on the point I mentioned? What I was talking was not about the object not having the reference but how could the compiler choose the correct function to be invoked if just the return types are different. It can be done by using reflection to find the type of the variable the result of the function is getting assigned to. But I guess thats why languages don't allow overloading based on return types alone.
Do you think I am right?
Joined: Oct 13, 2005
You are correct. If you allowed overloading on return types and required reflection to sort them out, think how slowly the language would execute.
We ahve been studying the concept of refinement recently, and that has the result that an overridden method must be refined by its overriding methods. That can only happne if they have compatible return types. If you have int method() in the superclass and double method() in the subclass, you can no longer say the subclass IS-A superclass.