• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

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

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic