java and protected/private/public

B

bigbinc

Is it true that using the protected keyword gives a variable more
access than the public keyword and 'not' using a access keyword
protects variables a little more than public.

I would like to give variables scope only to sub-classes of classes
but it seems I cant do that.


Berlin Brown
(e-mail address removed)

http://www.retroevolution.com
 
J

Joona I Palaste

bigbinc said:
Is it true that using the protected keyword gives a variable more
access than the public keyword and 'not' using a access keyword
protects variables a little more than public.

Not true at all. "Public" gives the most access. "Public" means that
anyone in the entire world can access it. "Protected" means only
subclasses and classes in the same package can access it. Not using a
keyword means that only classes in the same package can access it.
"Private" means that only the class itself can access it.
I would like to give variables scope only to sub-classes of classes
but it seems I cant do that.

I'm afraid you can't do that, Dave, I mean bigbinc. Too bad - I would
have wanted to as well. What in the world possessed Sun to implement
such a crazy idea in Java?

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Holy Banana of this, Sacred Coconut of that, Magic Axolotl of the other."
- Guardian in "Jinxter"
 
A

Adam Maass

Joona I Palaste said:
Not true at all. "Public" gives the most access. "Public" means that
anyone in the entire world can access it. "Protected" means only
subclasses and classes in the same package can access it. Not using a
keyword means that only classes in the same package can access it.
"Private" means that only the class itself can access it.


I'm afraid you can't do that, Dave, I mean bigbinc. Too bad - I would
have wanted to as well. What in the world possessed Sun to implement
such a crazy idea in Java?

All classes in the same package are friends to each other, in C++ terms.

You could achieve scope only in subclasses by placing your class in a
package by itself and declaring everything protected. (Not that that's a
particularly attractive solution.)

-- Adam Maass
 
J

Joona I Palaste

All classes in the same package are friends to each other, in C++ terms.

IMHO this is ugly design. Why model Java after C++? And even Stroustrup
told us not to use friends.
IMHO being a subclass of something is much simpler and nicer than being
in the same package as something.
You could achieve scope only in subclasses by placing your class in a
package by itself and declaring everything protected. (Not that that's a
particularly attractive solution.)

I agree this is not particularly attractive. In extreme cases you'd end
up with dozens of singleton packages.

--
/-- Joona Palaste ([email protected]) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"And according to Occam's Toothbrush, we only need to optimise the most frequent
instructions."
- Teemu Kerola
 
T

Tor Iver Wilhelmsen

I would like to give variables scope only to sub-classes of classes
but it seems I cant do that.

You could earlier, using "private protected" but it only confused
people so they removed it. The 1st ed "Java in a Nutshell" is the only
remaining reference to it I know of.
 
A

Adam Maass

Joona I Palaste said:
Adam Maass <[email protected]> scribbled the following:

[snip]
All classes in the same package are friends to each other, in C++ terms.

IMHO this is ugly design. Why model Java after C++? And even Stroustrup
told us not to use friends.

It starts to make sense if you consider a package a group of related classes
to solve a common problem -- they may need priveleged access to stuff that's
internal to the package but shouldn't be exposed to the world. Sure, you can
hide data members behind accessors, but you might want to declare methods
for use only within the package.

Of course, the common API packages java.lang and java.util don't even come
close to being packages in this sense. If java.util contained only the
collections framework classes and interfaces, it'd be much closer to this
concept.
IMHO being a subclass of something is much simpler and nicer than being
in the same package as something.

I don't completely agree. If the classes in a package are meant to be used
in a composition-style decarator pattern (java.io anyone?) then what's the
problem of package access? It is IMHO simpler than inheritence, and more
flexible to boot.

-- Adam Maass
 
B

bigbinc

Tor Iver Wilhelmsen said:
You could earlier, using "private protected" but it only confused
people so they removed it. The 1st ed "Java in a Nutshell" is the only
remaining reference to it I know of.

Thats an oxymoron.
 
D

Dale King

Joona I Palaste said:
Not true at all. "Public" gives the most access. "Public" means that
anyone in the entire world can access it. "Protected" means only
subclasses and classes in the same package can access it. Not using a
keyword means that only classes in the same package can access it.
"Private" means that only the class itself can access it.

And its nested/containing classes.
I'm afraid you can't do that, Dave, I mean bigbinc. Too bad - I would
have wanted to as well. What in the world possessed Sun to implement
such a crazy idea in Java?

You probably have the mistaken idea that C++ allows you to declare
varaiables that can only be accessed by subclasses. That is in fact, not
true. Protected members can also be accessed by friend classes. Package
members are the equivalent of friends in Java so Java is no worse than C++
in this reagard. Java did however make an improvement. In C++, friends have
access to everything even private things. Java at least gives you a way to
differentiate between those things that package members can access and those
things they can't. They have a more private access specifier than C++.
 

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

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top