For the following code why would Compiler throw an Error, why won't it be considered an overloading?
method() in Test cannot override method() in parent; attempting to use incompatible return type found: float required: int
Still Learing..
Jeff Storey
Ranch Hand
Joined: Apr 07, 2007
Posts: 230
posted
0
Jaspreet,
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:
hope this helps.
Jeff
Calum Ludwig
Greenhorn
Joined: Oct 01, 2007
Posts: 14
posted
0
Overloading has more to do with the method parameters.
Below is something I gleaned from a site:
overloaded methods can have the same name but must have different parameter lists
parameter lists are considered different if the order of the arguments are different
a subclass method can overload a superclass method
SCJP 1.4<br />SCJD
jaspreet atwal
Ranch Hand
Joined: Sep 05, 2007
Posts: 52
posted
0
Got yaa!! Thanks Jeff and Calum!!
Praveen Kumar
Ranch Hand
Joined: Nov 06, 2006
Posts: 133
posted
0
The no of args should differ for overloaded methods basically. That means overload methods have different return types in addition to this they should[must] differ in args.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
4
posted
0
Calum Ludwig gave the best answer. The return type is ignored in working out whether methods are overloaded or not.
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?
Nidhi Jain
Ranch Hand
Joined: Mar 26, 2006
Posts: 31
posted
0
The complier willbe invoked the function,that class have the refrence variable.
Suppose if I have clas A { void try() { // some code is here; } } class B extends A { int try() { // some code is here; } }
public static void main(String args[]) { A a=new A(); B b=new B(); A ab; ab. try()
}
So that will invoke the super class function please clear if i am wrong....
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
4
posted
0
Afraid I don't understand your code, Nidhi Jain. You will get at least 3 compiler errors: two for use of "try" as an identifier and one for not initialising the local variable ab.
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?
Cheers, Raj.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
4
posted
0
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.