aspose file tools*
The moose likes Mock Exam Errata and the fly likes Ambiguous question in Naveens test Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "Ambiguous question in Naveens test" Watch "Ambiguous question in Naveens test" New topic
Author

Ambiguous question in Naveens test

Jared Dahl
Greenhorn

Joined: Dec 05, 2001
Posts: 5
Naveen's SCJP site
http://www.javaprepare.c om/quests/test.html

Question 32 states:

If a base class has a method defined as
void method() { }
Which of the following are legal prototypes in a derived class of this class. Select all correct answers.

a. void method() { }
b. int method() { return 0;}
c. void method(int i) { }
d. private void method() { }

The answer key lists a and c as the correct answers. The question, however, is ambiguous
based on the fact that if the derived class is in a different package than the super class, D is correct also. If the derived class cannot see the method due to access modifiers, it cannot override it (therefore it hides or shadows it). I would re-word the question to state that the two classes are in the same package.


Jared Dahl
Nain Hwu
Ranch Hand

Joined: Sep 16, 2001
Posts: 139
Jared,
I agree with you.
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Jared,
The answer D can never be correct. You can't override any method to be more private.
Don't confuse access modifiers with compiler checks. All methods regardless of access modifiers are inherited by subclasses. The access modifiers are used just for that accessing. If the subclass is in another package the method is still inherited but can't be called. The compiler will still see you overridding a default modifier with a private modifier. I you had tried to call the method then you would also get a compiler error saying that the method is not visible to your program.
This is an important concept to understand. You might want to look into this as this is crucial in the SJPC.
Regards,
Manfred.
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Also,
You should get used to programming a simple example to prove which answers are correct. The simple example below should prove that D will get a compile error.

See if you can get B.java to compile.
Regards,
Manfred.
[This message has been edited by Manfred Leonhardt (edited December 05, 2001).]
Nain Hwu
Ranch Hand

Joined: Sep 16, 2001
Posts: 139
Hi, Manfred,

Don't confuse access modifiers with compiler checks. All methods
regardless of access modifiers are inherited by subclasses

I have to disagree with you on this. If a method is not accessible, it is not inherited. See JLS 8.4.6.
For example, a private method is not inherited.
And Jehred's question is another example.
Contrary to what you said, I got different result from
following code. They compiled and ran just fine.

Jared Dahl
Greenhorn

Joined: Dec 05, 2001
Posts: 5
Well, I did do a test class, and it did compile. I even was able to compile your example.
Several of the mock exams make a point of indicating that a final method can appear to be overridden if the subclass cannot see that final method because of the access modifier. Try compiling the following code.
// File A.java placed into dir: C:\temp\A.
package A;
public class A{
final void method()
{}
}
// File B.java placed into dir: C:\temp
import A.*;
public class B extends A{
private void method()
{}
}
This compiles just fine because method() in A is not overridden by method() in B. From page 107 of "Java in a Nutshell" - "It (a class) can also access the accessible fields and members it inherits from its superclass." If you can't access something, you can't override it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ambiguous question in Naveens test