aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Covariant return types & overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Covariant return types & overriding" Watch "Covariant return types & overriding" New topic
Author

Covariant return types & overriding

Jimmy Thomas
Greenhorn

Joined: Aug 17, 2005
Posts: 19
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

    Joined: May 29, 2003
    Posts: 1258


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


    Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
    Barry Gaunt
    Ranch Hand

    Joined: Aug 03, 2002
    Posts: 7729
    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.


    Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
    Getting someone to think and try something out is much more useful than just telling them the answer.
    Jimmy Thomas
    Greenhorn

    Joined: Aug 17, 2005
    Posts: 19
    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

    Joined: Sep 14, 1999
    Posts: 2813
    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 ]

    SCWCD: Online Course, 50,000+ words and 200+ questions
    http://www.examulator.com/moodle/course/view.php?id=5&topic=all
    Jimmy Thomas
    Greenhorn

    Joined: Aug 17, 2005
    Posts: 19
    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

    Joined: Sep 14, 1999
    Posts: 2813
    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...
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Covariant return types & overriding