aspose file tools*
The moose likes Java in General and the fly likes SCJP prep - protected method inheritence question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "SCJP prep - protected method inheritence question" Watch "SCJP prep - protected method inheritence question" New topic
Author

SCJP prep - protected method inheritence question

Gareth Anderson
Greenhorn

Joined: Feb 27, 2006
Posts: 13
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

Joined: Feb 27, 2006
Posts: 13
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 ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Gareth Anderson
Greenhorn

Joined: Feb 27, 2006
Posts: 13
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...)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SCJP prep - protected method inheritence question
 
Similar Threads
Protected Method called by 2nd subclass instance
method overriding and visibility promotion.
overriding JDBCRealm
Instance of an abstract class
Easy Protected Member Question