• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

SCJP prep - protected method inheritence question

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

Not sure which forum this was suited too so I'm posting it in intermediate, its not an easy question but I don't think my testing agrees with the answers in the study guide I'm reading for the Java 5.0 exam.

Anyway, this is an example question:
"True or false: If class Y extends class X, the two classes are in different packages, and class X has a protected method called abby(), then any instance of Y may call the abby() method of any other instance of Y."

Well the book is saying its false:
"An object that inherits a protected method from a superclass in a different package may call that method on itself but not on other instances of the same class.".

Well yes, I do realise from other programming (eg. C++) that protected means that subclasses have access to the method. But I disagree with the fact that it cannot be called on other instances of the same class.

I created this code to test it:

And this other class:


My testing is done in a new version of eclipse so it tells me if there are errors before I hit the compile button. There are no errors and it runs as expected printing:
"abby from x
abby from x"

And this is being run on a new instance of the same class. Does this contradict the question? (the answer would be true...)

These is similar text written in the chapter which says the opposite of what I'm thinking, and I'm confused (am I correct or misinterpreting things).

I have complied with java 5.0 compliance in eclipse (although I'm sure 1.4 is the same).

Thanks,
Gareth
[ February 27, 2006: Message edited by: Gareth Anderson ]
 
Gareth Anderson
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, here's the supporting text for the books answer (the opposite of my answer) from the chapter:

It�s important to be aware that different-package subclasses don�t have unlimited access to protected superclass features. In fact, different-package subclasses have only the following privileges:
They may override protected methods of the superclass.
An instance may read and write protected fields that it inherits from its superclass. However, the instance may not read or write protected inherited fields of other instances.
An instance may call protected methods that it inherits from its superclass. However, the instance may not call protected methods of other instances.


[ February 27, 2006: Message edited by: Gareth Anderson ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The book is simply wrong. Under no circumstances does Java make any distinction between "this object" and "other objects of this class" when making access control decisions.

There is an interesting subtlety in that, given a class A with a protected method m() and two subclasses B and C in some other package, an instance of B can call m() on itself and other B instances, but not on any instances of C. Just because B inherits m() doesn't give it special access to m() in other classes.
 
Gareth Anderson
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou for the information.

Yes I noticed that unusual difference, I assume that the SCJP certification isn't going to have any questions with that much detail.

For anyone who is interested I'm reading SCJP, SCJD - Complete Java 2 Certification 5th ed. From Sybex.

Hopefully they will fix this next edition (although the people writing the book are apparently the on the examination board for java certification...)
 
Without subsidies, chem-ag food costs four times more than organic. Or this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic