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.
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.
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.
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.