• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

access modifiers and constructor

 
Greenhorn
Posts: 13
Mac Mac OS X Mac PPC
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I have following two questions :

Why code at //1 is valid ? JVM will provide constructor for Parent class, that is not package private ? if not then what is access modifier of that constructor ?

Why code at //2 is invalid although parentMethod() is protected but Child extends Parent class so it should work ?
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jack McLaren wrote:
Why code at //1 is valid ? JVM will provide constructor for Parent class, that is not package private ? if not then what is access modifier of that constructor ?


It is protected. So any class in the same package AND any child of that class (or child of a child and so on...) might access it.

Jack McLaren wrote:
Why code at //2 is invalid although parentMethod() is protected but Child extends Parent class so it should work ?


Because my above statement only applies to inheritance (extending a class) and seeing a class as a whole (and that includes any protected constructors), but not accessing other class' fields or methods.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pawel Pawlowicz wrote:

Jack McLaren wrote:
Why code at //1 is valid ? JVM will provide constructor for Parent class, that is not package private ? if not then what is access modifier of that constructor ?


It is protected. So any class in the same package AND any child of that class (or child of a child and so on...) might access it.


Not quite. The constructor the JVM makes is a public, no arguments constructor, and is only made if you do not provide one yourself.
 
Paweł Baczyński
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steve Luke wrote:

Pawel Pawlowicz wrote:

Jack McLaren wrote:
Why code at //1 is valid ? JVM will provide constructor for Parent class, that is not package private ? if not then what is access modifier of that constructor ?


It is protected. So any class in the same package AND any child of that class (or child of a child and so on...) might access it.


Not quite. The constructor the JVM makes is a public, no arguments constructor, and is only made if you do not provide one yourself.


You are right. I was reading the code too fast and I "saw" a protected constructor there ;). My bad.
 
reply
    Bookmark Topic Watch Topic
  • New Topic