Results 1 to 7 of 7
Like Tree3Likes
  • 1 Post By sehudson
  • 2 Post By JosAH

Thread: question about return type of overriding the method

  1. #1
    thinkinjava24 is offline Member
    Join Date
    Sep 2012
    Posts
    17
    Rep Power
    0

    Lightbulb 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.

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    364
    Rep Power
    5

    Default Re: question about return type of overriding the method

    The method signature has to be different.
    Last edited by sehudson; 01-14-2013 at 11:52 PM.

  3. #3
    thinkinjava24 is offline Member
    Join Date
    Sep 2012
    Posts
    17
    Rep Power
    0

    Default 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.

  4. #4
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    364
    Rep Power
    5

    Default 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.
    Sierra likes this.

  5. #5
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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!

  6. #6
    thinkinjava24 is offline Member
    Join Date
    Sep 2012
    Posts
    17
    Rep Power
    0

    Default 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.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default 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,

    Jos
    DarrylBurke and Sierra like this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 2
    Last Post: 08-23-2011, 11:43 PM
  2. Invalid Method Declaration; Return Type Required
    By bremzb in forum AWT / Swing
    Replies: 3
    Last Post: 05-05-2011, 11:12 PM
  3. [SOLVED] Undeclared Return Type for Method?
    By fullmetaljacket in forum New To Java
    Replies: 11
    Last Post: 05-16-2009, 04:38 AM
  4. Method return type problem
    By McChill in forum New To Java
    Replies: 7
    Last Post: 05-05-2009, 10:21 PM
  5. Replies: 4
    Last Post: 11-25-2008, 06:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •