• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Himai Minh

protected access of class members

Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an interesting issue about protected access.
In Sun Certified Programmer for Java 6 Study Guide (Exam 310-065) book on page 39 there is
Table 1-2: Determining Access to Class Members:

Visibility Public Protected Default Private
1. From the same class Yes Yes Yes Yes
2. From any class in the same package Yes Yes Yes No
3. From a subclass in the same package Yes Yes Yes No
4. From a subclass outside the same package Yes Yes, through inheritance No No
5. From any non-subclass class outside the package Yes No No No

The table is 100% correct as far as it is determining access to class members declared in the class.
If you consider really any class members, especially inherited class members, I would split the 2. row
into two rows. One for declared members and the other for inherited members:

2a. declared members: Yes | Yes | Yes | No (same as original)
2b. inherited members: Yes | Yes, through inheritance | Yes | No

In other words it says: If you declare a protected member and inherit it,
it will be visible from any class in the same package only through inheritance.

It just reflects what is written on page 36:

"Once the subclass-outside-the-package inherits the protected member,
that member (as inherited by the subclass) becomes private to any code outside
the subclass, with the exception of subclasses of the subclass."

By the way my colleagues and I had always thought of protected access being "weaker" than
default access. But it is not true. For inherited members it is the opposite. Am I correct?

Thanks for a great book.

Posts: 9697
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy Ondrej, welcome to javaranch!!!

What you are saying is correct, but if you split the statement into 2a and 2b, then 2b will be the same as 4th statement. Suppose there is a class A in "a" package and class B in "b" package. B extends A. Now A declares a protected field length. As per statement 4, any class outside the package "a" will be able to access the length field only through inheritance. Statement 2b would mean the same but from the point of view of class B. The table lists how a field can be accessed based on the class in which it is declared. If it is modified in the way you want, then there will be redundancy.

Also I don't understand what you mean by "weaker" access, but default access is more restricted than protected access. And don't confuse yourself with inherited members. For class B, length is more restrictive than a default field that B declares, but if you see the class A in which it is actually declared, then protected is less restrictive. The restrictiveness is seen from the place (or class) in which the field or method is declared. I hope this makes sense to you...
Ondrej Cervinka
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ankit,
thanks for your welcome and reply. As you said, it is about the point of view. The first considers declared members only and the second any class members. Both of them are correct. The first is used int the book and is definitely easier to learn. It is more straight to think only about declared members when you want to determine the access level. It always works, so why one needs to get confused when thinking about inherited members. I just wanted to share the second point of view which should work for any class member (inherited and declared). But this is very tricky. By the way, if you consider the damned inherited members you can say that default access is less restricted than protected access:

As you said. If you see the restrictiveness (access level) from the class in which the field or method is declared then default access is always more restricted than protected access and many other things are more clear again.

That new kid is a freak. Show him this tiny ad:
Garden Master Course kickstarter
    Bookmark Topic Watch Topic
  • New Topic