This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
1 is definitely right. 2 is wrong as mathod can't be overloaded in subclass. it is overriding which takes place in subclasses at most once in each subclass. 3 is wrong as return type for overriding metheds must be same. In overloaded methods it may or may not be same but that being said, only difference in return type does not qualify for overloading. 4 is right, overriding method must not be less accessible than the original method it overrides otherwise whole purpose of overriding is defeated.
option 2 is correct. if two methods of a class(whether both declared in the same class,or both inherited by a class or one declared and one inherited)have the same name but different signatures,then the method name is said to be overloaded. This is given in JLS 8.4.7 Overloading.
option 3 is correct as compiler error will occur if they have same name and method signature but different return type.
option 4 is correct . if overriding method provides less access ,compile time error occurs.
please correct me if iam wrong.
Joined: Nov 02, 2004
Yes Jay.. that was my interpretation of the question..
so what should be the correct answer ??
Joined: Aug 27, 2004
I would say 2,3 and 4 are correct answers based on the above discussion. Moreover, I feel the wordings you suggested are correct
Each method in a parent class can be overridden at most once in any of its subclasses
All four are correct. I don't understand how anyone could contest it - there is no ambiguity, and the relevant specifications are quite explicit about each point. If you do wish to contest it, please provide some substance, otherwise, you should only be assumed incorrect. That is to say, if 6 billion people people believe the Sun is mostly hydrogen, but you believe otherwise, prove it; the authoritative source says otherwise.
Originally posted by Soumy Sam: This qn is posted in one of the mocks..
becoz as far as 1 question is concerned it would have been true if the qn is "Each method in a parent class can be overridden at most once in any of its subclasses"
Correct me if i am wrong with the interpretation of the question..
Yes, it does sound as if (1) is saying that any subclass can override a method regardless of its parent. So by qualifying the statement, as you proposed, with "its subclasses" clear things up. However, the statement did mention "parent class" at the beginning, so any mention of a subclass in that statement should mean the sublcasses of that parent, unless otherwise specified.
Joined: Aug 27, 2004
Soumy, as far as real exam is concerned you won't get questions which will put you in dilemma.