aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Packages 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 "Packages" Watch "Packages" New topic
Author

Packages

Simon Whitehouse
Greenhorn

Joined: Dec 10, 2001
Posts: 12
Hi Folks
The following is from JWhiz, test 1 Q3

Answers given are:
A. Compilation error at line 9
B. Compilation error at line 5
C. Runtime error at line 11
D. None of the above.
The answer is given as D as there will be a compilation error at line 11 as objX is not allowed to access any protected member of the SuperclassX.
Now, what has me baffled is that protected members " .. are accessible....by all subclasses of this class in any package where this class is visible." (from KM). The more that I look at this the more my head spins, can somebody please explain it to me?
Cheers
Simon
Arathi Rajashekar
Ranch Hand

Joined: Nov 20, 2001
Posts: 112
Hi Simon,
You are forgetting one point here. The protected variables are accessible, even though the subclass resides in diffrent package. But those variables are accessible, only by using the reference of the subclass. That is if you change the above code to objY.superClassX. it will compile fine. Hope it helps you


Arathi<br />Sun Certified Java Programmer
Rick Salsa
Ranch Hand

Joined: Jul 17, 2001
Posts: 173
Originally posted by Simon Whitehouse:
Hi Folks
11.i = objX.superclassVarX;
[/CODE]
Answers given are:
A. Compilation error at line 9
B. Compilation error at line 5
C. Runtime error at line 11
D. None of the above.
The answer is given as D as there will be a compilation error at line 11 as objX is not allowed to access any protected member of the SuperclassX.

I think you mean C. That would be correct as a subclass can only reference protected members using its own type or subtype. The problem with line 11 is that the variable is a reference to the super class, you can't access a protected member that way.
/rick
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Simon
Check out this link for a previous discussion on this topic.
There have been many others too. Click on the search link at the top of this page and type in 'protected implementation' (without the quotes) as your search words, you'll get a few other links that might help you out.
hope that helps


Dave
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
C is wrong since the error occurs at compile-time and not runtime... That's a trap. Watch out for those questions!


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Arathi Rajashekar
Ranch Hand

Joined: Nov 20, 2001
Posts: 112
Hi,
Is it legal to access a method of superclass using subclass reference variable as how it is done below. It is just the above code which is posted by other member

When we try to call subclassMethodY() from public staic void main application, it gives compiler error. I am right or wrong
[ January 15, 2002: Message edited by: Arathi Rajashekar ]
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
Hi all,
U know what? even if u make this code compile it wont work and give u "OutOfMemoryException" at runtime.
as u r having class Y variable initialized in the class Y itself. so when it calls Y's constructor it will recursively call it for

SuperclassX objX = new SubclassY();
statement in class Y...
this question i saw sometime ago on this forum having this "OutOfMemoryException"....
regards
maulin.
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Well it's darn good thing it won't compile then isn't it?
Here's another thread talking about the exact same kind of issue right now:
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=013951

Rob


Rob
SCJP 1.4
Simon Whitehouse
Greenhorn

Joined: Dec 10, 2001
Posts: 12
Okay then. Thanks to everybody who has posted replies so far. I've had a look at a couple of other threads and I think that I'm finally getting somewhere.
I think that if, instead of line 11 in the code that I originally quoted, I replaced it with :-

Then the code would compile although I'm a bit hazy as to exactly why.
I'm getting very confused about the idea that the subclass can only access superclass variables in this example if they create and use that superclass member themselves.
My problem is exacerbated because I can't get the code to compile. I have separated the two files, saved them to my Java directory on the hard drive and compiled superclassX.java. When I try to compile subclassY then the compiler complains that it can't find packageX in the import statement and that it can't find superclassX.
Any further help much appreciated.
Finding this bit dog-darn difficult and looking forward to a yee-ha moment soon ......
Simon
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
For the final exhilarating just read JLS 6.6.2


SCJP2. Please Indent your code using UBB Code
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Packages