• 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

Protected Constructors

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In experimenting with various combinations of access modifiers, I came across an apparent rule that I haven't seen in any books. It seems that if a child class tries to construct an instance of its parent (directly, not via inheritance) it'll work fine, unless the invoked constructor is protected and the parent and child are in different packages. Example:
The Parent1() call generates: Child1.java:5: Can't access protected constructor of class com.study.subpackage.Child1. Instance creation is permitted only within the package in which the constructor is defined.
Is there a rule I'm missing? Are there other similar cases?
 
Bill Compton
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting followup: If, on the other hand, the Parent1() constructor (see above) gets invoked from the context of a Child1() constructor, it works dandy:
Strange...?
 
Trailboss
Posts: 23780
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It all makes sense to me.
Protected means that the child has access to the parent. In the first case you are trying to instantiate a new object that is not part of the current relationship. A sort of "has a" relationship instead of a "is a" relationship. "protected" is for "is a" relationships only.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, and after all constructors are like other instance methods and so they are subjected to the usual access modifier restrictions.
Good finding though ..
------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
 
Bill Compton
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the explanation. That helps.
 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bill,
We had an earlier topic similar to yours (I think).
http://www.javaranch.com/ubb/Forum33/HTML/001321.html
-Peter
[This message has been edited by Peter Tran (edited January 26, 2001).]
 
Bill Compton
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep, the above thread is very similar. The key to understanding both (I think) is: "protected" access only grants access to child classes in other packages if reference is attempted via inheritance. That is, an explicit reference to Parent.Method() bypasses inheritance and is therefore disallowed. However, if the child simply invokes Method(), this is allowed.
 
reply
    Bookmark Topic Watch Topic
  • New Topic