Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes access Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "access" Watch "access" New topic


shailu kneni

Joined: Feb 02, 2004
Posts: 7
If I have a variable which I declare as protected it can be accessed by only derived claases from my class as well as all those in the same package. Is that right? In that sense protected is more public than the default access where only package members can access the variables but not also the derived classes. Bit confusing.

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Hi Shailu,
Welcome to JavaRanch!
Yes, you're right, it is more public than the default. The levels in Java are
The "private", "protected," and "public" levels are designed to be similar to the like-named levels in C++. Since C++ doesn't have packages, Java's "default" level is more similar to C++'s "private" than anything else. This might help you remember.

[Jess in Action][AskingGoodQuestions]
shailu kneni

Joined: Feb 02, 2004
Posts: 7
Hi Ernst,
Thanks for your quick response. I am the same Shailu with the name Shailu Kommineni but for some reason it was not allowing me to login with the name so I created another handle.
I feel the access modifier "protected" is confusing. It is the most public of the modifiers only next to public.

Nischal Topno
Ranch Hand

Joined: Nov 24, 2001
Posts: 45
you can take the help from the following table

[ Jess added UBB [code] tags to preserve whitespace, check 'em out! ]
[ February 05, 2004: Message edited by: Jessica Sant ]
Masha Klimenko
Ranch Hand

Joined: Feb 04, 2004
Posts: 31
Protected and default access levels are similar. The only difference
between the two is that protected members are inherited by subclasses
outside the superclass package, whereas members with default access are not. Deafult members can only be inherited by subclasses that are in the same package as superclass. Think of it this way: protected makes provisions for the kids (subclasses) outside the family (package), and default only makes provisions for the kids in the family (package).
Hope this helps.

I agree. Here's the link:
subject: access
jQuery in Action, 3rd edition