• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

access

 
shailu kneni
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Shails
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shailu,
Welcome to JavaRanch!
Yes, you're right, it is more public than the default. The levels in Java are
private
(default)
protected
public
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.
 
shailu kneni
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Thanks,
Shails
 
Nischal Topno
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic