Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Covariant return types & overriding

 
Jimmy Thomas
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was taking one of the mock exams (SCJP 5), and I found the following question:
Which of the following statements are true?
  • A method cannot be overloaded to be less public in a child class
  • To be overridden a method must have the same name and parameter types
  • To be overridden a method must have the same name, parameter and return types
  • An overridden method must have the same name, parameter names and parameter types

  • According to the author the answer is 3.

    But now with Java 5's covariant return types, does an overriding method need to have the same return type. Kindly advice...

    I also tried out a sample program.


    Thanks,
    Jimmy
     
    Nathaniel Stoddard
    Ranch Hand
    Posts: 1258
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator


    Technically, these two functions do have the same type (Object -- since all Strings are Objects). I'm not sure if I would use the same wording that you quoted. I would say that all methods should have return types that are directly related to each other (that is, each should be a sub-type or super-type of all the others).
     
    Barry Gaunt
    Ranch Hand
    Posts: 7729
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Interesting question. I think you are correct. A reference type returned by an overriding method must be the same as, or a subtype of, the reference type returned by the overridden method. I suggest that you contact the author of the mock exam question.
     
    Jimmy Thomas
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Nathaniel & Barry, I had chosen option two & got it wrong...

    This question is from Marcus Green's examulator, Question Bank Id:713.

    -- Jimmy
     
    Marcus Green
    arch rival
    Rancher
    Posts: 2813
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for bringing this up Jimmy, and particularly for including the quesitonid. When I was first creating those questions I had overlooked that change to the JDK, and did not understand the use of the term covariant in the objecive 1.5 thus...

    "Objective 1.5
    Given a code example, determine if a method is correctly overriding or overloading another method, and identify legal return values (including covariant returns), for the method."


    The folks at WhizLabs mention this at

    http://www.whizlabs.com/articles/scjp5.0-article.html
    And they have a pointer to a useful article at

    http://java.sun.com/developer/JDCTechTips/2004/tt1201.html#2


    I will update that question to match the reality of JDK 1.5. Again many thanks for the feedback.
    [ September 10, 2005: Message edited by: Marcus Green ]
     
    Jimmy Thomas
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Marcus,

    It's always a pleasure to help you guys out.

    Do the questions on your free mock exams get randomly selected out of 320 questions or are they a fixed set of 60 questions??

    Regards,
    Jimmy
     
    Marcus Green
    arch rival
    Rancher
    Posts: 2813
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    They are from a pool of 60 question marked as guest questions from the total bank. However "every so often" I change those 60 questions so I get a bigger audience for feedback on my questions than purely those who have subscribed. I just checked and the total question count in the whole JDK 1.5 database is 334 at the moment and I have some more to add in the near future. However quality beats quantity so I try not to rush at it ....

    I have been working pretty much full time on JDk 1.5 SCJP material for several months now...
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic