Results 1 to 7 of 7
- 01-14-2013, 09:48 PM #1
Member
- Join Date
- Sep 2012
- Posts
- 17
- Rep Power
- 0
question about return type of overriding the method
I met a problem when preparing the OCPJP, the problem is about overriding the method. here is the code:
class Lotest {
public int getInt() {
System.out.println("super");
return 1;
}
}
public class Oltest extends Lotest {
public float getInt() {
System.out.println("sub");
return 1.0f;
}
}
is this legal?
personally, I think it is illegal, since you can not override the method with changing the return type, however, the answer from the book is that this is fine from J2SE5.0, So I need to make sure of that. hope anyone could help me.. thanks a lot.
- 01-14-2013, 10:44 PM #2
Re: question about return type of overriding the method
The method signature has to be different.
Last edited by sehudson; 01-14-2013 at 10:52 PM.
- 01-14-2013, 10:56 PM #3
Member
- Join Date
- Sep 2012
- Posts
- 17
- Rep Power
- 0
Re: question about return type of overriding the method
So, you think this is illegal, right?
hope anyone can recommend some book that I can prepare for the OCPJP, I found so many book online but containing so much mistake....what a pity.
- 01-14-2013, 11:00 PM #4
Re: question about return type of overriding the method
No, I don't think that what you have done is legal. If the arguments that your getInt() method in your subclass took were different, this would be ok. Or, if the method signature was exactly the same, meaning the getInt() method in your subclass returned an int, and you used the @Override notation, you would be fine also. Look out for more responses though.
- 01-14-2013, 11:31 PM #5
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: question about return type of overriding the method
It is illegal as sehudson says - you cannot change the type of a method by overriding like that. It is correct that a method is considered the same if it has the same method name, number of arguments and types of arguments.
To make it clearer the @Override annotation should be used here with getInt(). As is the code is not compiling.I like likes!.gif)
- 01-15-2013, 12:24 AM #6
Member
- Join Date
- Sep 2012
- Posts
- 17
- Rep Power
- 0
Re: question about return type of overriding the method
Thank you for your response, my man!
the bad book almost mislead me.
can you recommend some reliable book for me to prepare the OCPJP?
also thanks Sierra.
- 01-15-2013, 07:33 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: question about return type of overriding the method
About the return types of overriding methods: before Java 1.5 a method m( ... ) that wants to override a method m( ... ) in a super class had to have the same signature as the method in the super class and exactly the same return type; starting from Java 1.5 this is not needed anymore, i.e. if m( ... ) in the super class returns a type T, the method m( ... ) in the sub class can return a type S as long as S is a sub class of T (because an S *is a* T, the term is 'covariant return type').
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Invalid method declaration; return type required? Please Help!
By paccerz in forum New To JavaReplies: 2Last Post: 08-23-2011, 10:43 PM -
Invalid Method Declaration; Return Type Required
By bremzb in forum AWT / SwingReplies: 3Last Post: 05-05-2011, 10:12 PM -
[SOLVED] Undeclared Return Type for Method?
By fullmetaljacket in forum New To JavaReplies: 11Last Post: 05-16-2009, 03:38 AM -
Method return type problem
By McChill in forum New To JavaReplies: 7Last Post: 05-05-2009, 09:21 PM -
Help: how can get method, arg, return type... from a file .class
By ykzforever in forum New To JavaReplies: 4Last Post: 11-25-2008, 05:53 AM


3Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks