4 Replies - 774 Views - Last Post: 11 October 2010 - 05:06 PM Rate Topic: -----

#1 mcclane400  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 63
  • Joined: 25-March 09

Protected visibility

Posted 11 October 2010 - 11:04 AM

A protected variable is accessible to subclasses and other classes within the same package. My question is, has it always been like this? I could have sworn that I learned that protected meant only subclasses could access protected variables. Did this change in an update of Java or was it always this way?
Is This A Good Question/Topic? 0
  • +

Replies To: Protected visibility

#2 javadork  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 135
  • Joined: 21-August 10

Re: Protected visibility

Posted 11 October 2010 - 11:26 AM

This has always been the case. From the 1st edition of Java Language Specification:

Otherwise, if the member or constructor is declared protected, then access
is permitted only when one of the following is true:
* Access to the member or constructor occurs from within the package
containing the class in which the protected member is declared.
* Access occurs within a subclass of the class in which the protected
member is declared, and the access is correct as described in 6.6.2.


Was This Post Helpful? 0
  • +
  • -

#3 mcclane400  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 63
  • Joined: 25-March 09

Re: Protected visibility

Posted 11 October 2010 - 11:41 AM

Apparently I've written way too much C++ code in the last few years because you can't do this in C++. I thought I learned that only subclasses could access protected variables in Java. Either my memory is bad or my professor didn't mention it. Either way, now I know.

This post has been edited by mcclane400: 11 October 2010 - 11:57 AM

Was This Post Helpful? 0
  • +
  • -

#4 javadork  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 32
  • View blog
  • Posts: 135
  • Joined: 21-August 10

Re: Protected visibility

Posted 11 October 2010 - 11:51 AM

I think that's generally the case when protected access is taught, people tend to ignore, whether intentionally or not, the part about package access. I certainly don't recall it ever being brought up, I only recently in the past year or so learned the nitty gritty details about it from Eckel's Thinking In Java.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Protected visibility

Posted 11 October 2010 - 05:06 PM

View Postmcclane400, on 11 October 2010 - 12:41 PM, said:

Apparently I've written way too much C++ code in the last few years because you can't do this in C++. I thought I learned that only subclasses could access protected variables in Java. Either my memory is bad or my professor didn't mention it. Either way, now I know.

Yes writting too much C++ code is not good for your brain
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1