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