aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A ques's answer explanation not correct in K&B in chap 2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A ques Watch "A ques New topic
Author

A ques's answer explanation not correct in K&B in chap 2

Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
hi all,

K&B chap 2 page 136 ques 6:

Given the following,

which statement is true?
A. The code compiles and runs, with output this 420 parent 420.
B. If line 8 is removed, the code will compile and run.
C. If line 10 is removed, the code will compile and run.
D. Both lines 8 and 10 must be removed for the code to compile.
E. An exception is thrown at runtime.

well undoubtedly the answer is c as a subclass outside the pacakge of the superclass can access the protected member only by inheritance i.e. it can only access them by object reference of type the sub class.

the explanation given says:



now if we go according to its explanation that
doStuff() can be accessed only by instances of the ChildUtil class


p should have accessed the protected member as its an instance of the class ChildClass.

in my opnion, the reference type used for such an acces should be of type subclassor its further subclasses.

plz verify my thought, i'll be deeply obliged.

thanx
amit

[edited because unindented code is too painful to read - Jim]
[ April 28, 2005: Message edited by: Jim Yingst ]
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
You are right that their explanation was not totally complete. You showed a counterexample where doStuff() appears to be getting accessed by an instance of ChildUtil, but it does not compile because p is a ParentUtil reference. Since p.doStuff() is not a legal call from within ChildUtil, you get a compiler error.

Here's the tricky part: the compiler doesn't know or care that p actually references a ChildUtil object. All the compiler knows or cares about is that p is a reference to a ParentUtil, and calling the doStuff() method on a ParentUtil is not legal from that point in the code. It looks like it should work because of polymorphism, but actually polymorphism doesn't apply here because you're not allowed to make the call p.doStuff() at all, regardless of what p actually references.

What I suspect the authors meant by "doStuff() can be accessed only by instances of the ChildUtil class" was that you would need an instance of ChildUtil that is being referenced by a ChildUtil reference as well. They simply neglected to point out the case you presented, where the reference type differs from the instance type.
[ April 28, 2005: Message edited by: Joe Sanowitz ]

SCJA 1.0 (98%), SCJP 1.4 (98%)
Saranyan Narayanan
Ranch Hand

Joined: Mar 12, 2003
Posts: 34
Joe could we say this:

When a protected member is accessed in a subclass using a parent class reference , it cannot let the polymorphic call compile as the protected member in the overriding class is private to the subclass.
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
Originally posted by Saranyan Narayanan:
Joe could we say this:

When a protected member is accessed in a subclass using a parent class reference , it cannot let the polymorphic call compile as the protected member in the overriding class is private to the subclass.


Careful with the terms "overriding" and "overridden". People often confuse them. The overriding method is the one in the subclass that overrides the original parent method. The overridden method is the original parent method that the child has replaced (overridden) with a new version.

Anyway, in the examples Amit Das posted, there is no overriding. Subclassing is not the same thing as overriding. You can override an accessible method, but you cannot override a class.

I wouldn't say "When a protected member is accessed in a subclass using a parent class reference..." Instead I would say:

If the child class and parent class are in different packages, a protected member of the parent class cannot be accessed in the child class by using a parent class reference.

While you're getting on the right track in your thinking, it's not really correct to say that the method is "private to the subclass", since "private" has a distinct meaning. I prefer the word "inaccessible". The method is inaccessible to the subclass. I also wouldn't say "it cannot let the polymorphic call compile" because there is no polymorphic call here. Polymorphic calls are possible only when you can invoke the method on the parent reference. Since p.doStuff() is not legal in this situation, it's simper and more accurate to state the rule as I did in bold in the previous paragraph. Here's the rule again with sub- and super- terminology if you prefer:

If the subclass and superclass are in different packages, a protected member of the superclass cannot be accessed in the subclass by using a superclass reference.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A ques's answer explanation not correct in K&B in chap 2