Question on default & protected member access

L

lonelyplanet999

Hi,

I'm studying Java Sun certified programmer (310-035) stuff and met
below questions:

1. What is the most restrictive access modifier that will allow
members of one class to have access to members of another class in the
same package ?

A. public
B. abstract
C. protected
D. synchronized
E. default access

The book's answer is E. It just said that default access is the
"package oriented" access modifier and public and protected are less
restrictive. But from what I know, access restriction among default
access & protected or public access are no different within a package.
Am I correct ? If not, could anyone suggest a counter example because
I couldn't think of one up to now.
 
M

Michael Borgwardt

lonelyplanet999 said:
1. What is the most restrictive access modifier that will allow
members of one class to have access to members of another class in the
same package ?

A. public
B. abstract
C. protected
D. synchronized
E. default access

The book's answer is E. It just said that default access is the
"package oriented" access modifier and public and protected are less
restrictive. But from what I know, access restriction among default
access & protected or public access are no different within a package.
Am I correct ?

Sure, but the question is what is the *generally* most restrictive modifier,
that (side condition) allows access to another class in the same package.
public and protected allow that, but also allow other classes (subclasses
in the case of protected) to access the member, thus they are both
less restrictive than defaul access.
 
L

lonelyplanet999

Michael Borgwardt said:
Sure, but the question is what is the *generally* most restrictive modifier,
that (side condition) allows access to another class in the same package.
public and protected allow that, but also allow other classes (subclasses
in the case of protected) to access the member, thus they are both
less restrictive than defaul access.

I still couldn't see the 'real' difference since for a class defined
inside a package, it's public & protected members can be accessed by
any other classes inside the same package, include it's subclass which
can be seen as 'other' class. For a class' default members, it can
also be accessed from any other classes inside the same package,
including subclass of that class or subclass of other classes should
that class defined instance of this class with default members.
 
M

Michael Borgwardt

lonelyplanet999 said:
I still couldn't see the 'real' difference since for a class defined
inside a package, it's public & protected members can be accessed by
any other classes inside the same package, include it's subclass which
can be seen as 'other' class. For a class' default members, it can
also be accessed from any other classes inside the same package,

But *not* from subclasses of that class in another package.
Protected allows such access and is thus less restrictive.
 
J

John C. Bollinger

[clja removed from list]
I still couldn't see the 'real' difference since for a class defined
inside a package,

All classes are in packages. If no package is explicitly specified for
a class then it's in a default package, of which there can in principle
be more than one.
it's public & protected members can be accessed by
any other classes inside the same package, include it's subclass which
can be seen as 'other' class. For a class' default members, it can
also be accessed from any other classes inside the same package,
including subclass of that class or subclass of other classes should
that class defined instance of this class with default members.

Yes, but you are still missing the point. Perhaps the lynch pin here is
that a class may have subclasses in different packages. Such subclasses
can access their parents' public and protected members, but not their
parents' default-access and private members. As a result, making a
member protected is almost as open as making it public: anyone who has
access to the class itself can get access to its protected members
simply by subclassing it.

Try this, for example:

package foo;

public class Foo {
protected int forFoosEyesOnly;
}

----

package bar;

public class FooSpy {

static class DoubleAgent extends foo.Foo {

public static int getFooData(Foo f) {
return f.forFoosEyesOnly;
}
}

public int spyOnFoo(Foo f) {
return DoubleAgent.getFooData(f);
}
}


John Bollinger
(e-mail address removed)
 
D

David Zimmerman

John said:
Yes, but you are still missing the point. Perhaps the lynch pin here is
that a class may have subclasses in different packages. Such subclasses
can access their parents' public and protected members, but not their
parents' default-access and private members. As a result, making a
member protected is almost as open as making it public: anyone who has
access to the class itself can get access to its protected members
simply by subclassing it.

Only if the class is subclassable. If the class is final or if the
constructors are restricted (EG private) then you can't build a
subclass. hence, you can't call the restricted methods
 
B

brougham5

The book's answer is E. It just said that default access is the
"package oriented" access modifier and public and protected are less
restrictive. But from what I know, access restriction among default
access & protected or public access are no different within a package.
Am I correct ?

For the test, it's probably better to memorize the order of least
restrictive to most restrictive instead of trying to reason it out.

Default access is more restrictive than protected, and it will satisfy the
question. Within the package, you are correct. But outside the package,
default access is more restrictive. So overall, default is more
restrictive. Does this make sense?
 
J

John C. Bollinger

David said:
Only if the class is subclassable. If the class is final or if the
constructors are restricted (EG private) then you can't build a
subclass. hence, you can't call the restricted methods

Yes, that was an implicit assumption. If a class is written so that it
is impossible to write a subclass in another package then protected
members are no more accessible than default-access members, but that
defeats the purpose of assigning protected access in the first place.


John Bollinger
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top