• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

can't protected constructor

 
Ranch Hand
Posts: 59
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
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

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
 
Marshal
Posts: 76854
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
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

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: 76854
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for my mistake.
 
Campbell Ritchie
Marshal
Posts: 76854
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Alas, poor Yorick, he knew this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic