This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

can't protected constructor  RSS feed

 
s sivaraman
Ranch Hand
Posts: 59
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,





error :

i shouldn't get that as it's legal to access a protected member(constructor) from a subclass though it's present in different package.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s sivaraman wrote:
error :

i shouldn't get that as it's legal to access a protected member(constructor) from a subclass though it's present in different package.


Yeah, you forgot about the "responsible for implementation" requirement.

https://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#jls-6.6.2

And you can easily test this by instantiating an AccessModifiers instance, and notice that the protected constructor of the superclass is called during instantiation.

Henry
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A constructor is not a member of the class, but the access regulations are the same as for members. The bit about responsible for implementation means inside a subclass object (to a first approximation), and a static method is not part of the object. Protected access does not mean access within static members of subclasses.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:A constructor is not a member of the class, but the access regulations are the same as for members. The bit about responsible for implementation means inside a subclass object (to a first approximation), and a static method is not part of the object. Protected access does not mean access within static members of subclasses.


I don't think that I agree with this. The JLS, IMO, is referring to the executing code as the entity that is required to be "responsible for the object". This means any code; constructors, instance or static methods, instance or static initializers, or heck, even code in nested classes of the top-level class.

The concept is ... the executing/accessing code is from the AccessModifiers class, so the code must be working with some instance that IS-A AccessModifiers class (hence, it is "responsible" for it) -- and if it was, it is allowed to access protected members of that instance.

Unfortunately, in this case, it is constructor, which makes it harder to envision. If it was a protected method (or protected field) of the super class, it would have been easier to see that the static method can access it, provided it had an instance that IS-A AccessModifiers class.

Henry

 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for my mistake.
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried it and couldn't get things to compile:
javac -d . SuperClass.java SubClass.java
SubClass.java:19: error: SuperClass(int) has protected access in SuperClass
System.out.println(new SuperClass(123).i);
^
SubClass.java:19: error: i has protected access in SuperClass
System.out.println(new SuperClass(123).i);
^
2 errors
Both the constructor (line 8) and the field i (line 13) are accessible in the subclass constructor and the instance method but neither appears accessible to the static main method. If I change line 19 to new SubClass&hellip, that constructor is accessible to the main method and therefore the i field is accessible to the SubClass object and it compiles nicely.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!