• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Packages

 
Simon Whitehouse
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rick Salsa
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C is wrong since the error occurs at compile-time and not runtime... That's a trap. Watch out for those questions!
 
Arathi Rajashekar
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Simon Whitehouse
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the final exhilarating just read JLS 6.6.2
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic