Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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 ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

A question regarding the protected member access

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am pretty sure the following question has been asked before but I couldn't find an answer make sense to me in this forum. The question is from Khalid's SCJP book' review question (ch 4.18)




The answer is lines (1), (3), (4), (5), and (8) will compile. I understand why (1), (5) will compile and (2), (6), (7), (9), (10), (11), (12) WON'T compile because those are clearly explained in the K&B SCJP book. What I find difficult to understand is why, for example in line (3), you can use a subclass reference to access protected member inherited from the superclass. Even stranger is that you can use a reference of C (subclass of B ) to access the variable pf from class A. I know these are facts because I have verified the code using Eclipse. However, I find it difficult to believe because it is stated in K&B book

For a subclass outside the package, the protected member can be accessed only through inheritance.



That's how I rule (2), (6) and (7).

Regards,

-Cheung


 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Cheung,
Protected instance member is accessible through inheritance in sub class of other package but you can't access using super class reference from sub class.
You can access inherited member through sub class reference it is allowed.
Other classes from same package of sub class can't access protected and inherited member of super class of sub class using reference of sub class.
But if protected member is static then it can be accessible to sub class using super class reference.
Correct me if I am wrong.
 
Cheung Chau
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ninad,

I think you are absolutely correct. However, I am looking for any reasoning behind it. Here is something I came up with after I submitted the original post: all the non-static member variable in a class can be explictly refered to using "this" keyword, which mean current object reference. That mean



refering to the same variable in my example class B or C. Because "this" is representing a instance reference B or C, I think it makes sense that by using obj2 or obj3, we can access pf. I know the logic may be a little broken. For example, It doesn't explain why you can access pf in Class B using obj3, an reference to an instance of Class C.

Also, I am really gald that you brought up

But if protected member is static then it can be accessible to sub class using super class reference.




From a post in this forum, we



You will get:
test.java:10: x4 has protected access in packageA.pack
System.out.println( p.x4 );

We see that we can use p or pack or even test to access x2, but an attempt to access x4 using p failed. As you can see, I understand the rule but don't know why.

Thanks

-Cheung

P.S. Of course, the simplest answer will be JVM or the Java compiler behavior this way
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

P.S. Of course, the simplest answer will be JVM or the Java compiler behavior this way



Yup.... That's the answer.

See section 6.6.2 in the Java specification.... http://java.sun.com/docs/books/jls/third_edition/html/names.html

Henry
 
Cheung Chau
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry,

Thank you for your reply. I do believe sometime there is no good explaination on certain rule in java. This is possibly one of those cases. However, I was hoping there were some logical explaination behind this. Just like if someone asks me why you can't access non-static variable from static method, I would quote K&B

A static method can't access a nonstatic (instance) variable, because there is no instance! That's not to say there aren't instances of the class alive on the heap, but rather that even if there are, the static method doesn't know anything about them. The same applies to instance methods; a static method can't directly invoke a nonstatic method. Think static = class, nonstatic = instance. Making the method called by the JVM (main()) a static method means the JVM doesn't have to create an instance of your class just to start running code.



I believe Java Spec specify this too but somehow this paragraph engraves this fact into my brain. Anyhow, thank your for your time. I am very interested in reading your java thread book in a near futre ;)

-Cheung
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thank you for your reply. I do believe sometime there is no good explaination on certain rule in java. This is possibly one of those cases. However, I was hoping there were some logical explaination behind this.



Well, if you think about it a bit, it does make sense, doesn't it?

In Java, the private access modifier is class based -- meaning private is private for the class, and not just for the instance. Obviously, public can also be accessed by the class, although any instance of any class can access it.

This is no different for the protected access modifier. Any instance can access the protected field of any other instance that IS-A that class. So, an instance of B can access the protected modifier of another B (and C, which of course, IS-A B).

Why would you think this behavior is not logical?

Henry
 
Cheung Chau
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry,

Thank for the explianation. That makes sense. How about why a static protected variable behavior differently from a non-static one. More specifically, why we can use a parent reference or class name to access a static protected variable?

Thank for your time again.

-Cheung
 
reply
    Bookmark Topic Watch Topic
  • New Topic