File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overriding doubt ? 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 "Overriding doubt ?" Watch "Overriding doubt ?" New topic
Author

Overriding doubt ?

Soumy Kumar
Ranch Hand

Joined: Nov 02, 2004
Posts: 78
This qn is posted in one of the mocks..


I am quite sure that the answers are 2, 3, 4
But the answer given was all 4.

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


SCJP 1.4<br />" Something is difficult doesn't mean you shouldn't try, it only means you should try harder "
Amit Saini
Ranch Hand

Joined: Oct 20, 2004
Posts: 280
2 is wrong. method can be overloaded only in the same class
3 is wrong because return type is not a part of method signature

hope that helps.
amit
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
All the four are correct:
1) Any subclass of a parent class can contain only one version of the overridden method, how can u have two methods having the same singnature in a class

2) Methods can be overloaded in subclass too. Look here

here

3 and 4) Thats quite obvious, thats what overriding is all about
Soumy Kumar
Ranch Hand

Joined: Nov 02, 2004
Posts: 78
2 is wrong. method can be overloaded only in the same class




Is the method add() method defined in subclass not considered overloading??
correct me if wrong..
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Amit ,
you are completely wrong . all 4 are right .
Soumy Kumar
Ranch Hand

Joined: Nov 02, 2004
Posts: 78
hello animesh..

the qn is
(1) Each method in a parent class can be overridden at most once in any one subclass

I accept that "No two methods can have same signature in a class"

Is this statement TRUE
"Each method in a parent class can be overridden at most once in any one or ALL subclass"
Veer Batra
Ranch Hand

Joined: Mar 12, 2001
Posts: 35
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.
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
Soumy,
I understand your argument on the wordings of the option 1. Here is the sample code


Now according to choice 1 , here fillGas() is not overriden in class Horse which is a subclass. ( any one subclass )

Soumy, Is that what you meant ?
[ March 03, 2005: Message edited by: Jay Pawar ]

Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
vinuharan haran
Ranch Hand

Joined: Feb 26, 2005
Posts: 64
options 2,3,4 are correct.

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.
Soumy Kumar
Ranch Hand

Joined: Nov 02, 2004
Posts: 78
Yes Jay.. that was my interpretation of the question..

so what should be the correct answer ??
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
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

"it's " is necessary
D'Angelis Grant
Greenhorn

Joined: Feb 24, 2005
Posts: 9
I'd say that all answers are correct.

1. How can any one class override the same method more than once?

2. You can still overload a method in a sub class.
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
All 4 are correct.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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.


Tony Morris
Java Q&A (FAQ, Trivia)
Amit Saini
Ranch Hand

Joined: Oct 20, 2004
Posts: 280
i apologize. my bad.
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
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.
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
Soumy, as far as real exam is concerned you won't get questions which will put you in dilemma.
Soumy Kumar
Ranch Hand

Joined: Nov 02, 2004
Posts: 78
thanks ..all..
 
Consider Paul's rocket mass heater.
 
subject: Overriding doubt ?
 
Similar Threads
method overriding qn.
Interface qn
Innerclass Qn
Problem with tipsmart q24
Qn on Threads