• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Ambiguous question in Naveens test

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jared,
I agree with you.
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic