Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Difference between a protected and package-private (no modifier) constructor?  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why do I get an error?



 
Marshal
Posts: 60136
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I hope you don't use that sort of name for your classes elsewhere; that is a sure‑fire source of confusion.
Please tell us what sort of error you are getting. Is the code failing to compile? Have you looked up what protected means? Try here. Is the declaration part of the implementation of the subtype?
 
Bartender
Posts: 1876
46
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also adding to what Campbell asked,
"package" should be the first line of your source code. Also, the directory where you create your java source code matters. You cannot define two packages in the same java source.
 
Sheriff
Posts: 5121
138
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Karla Carr
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




I have specified in the comments in the code where I have been getting an error in my code.
Could someone please explain me why the instantiation doesn't work?
Also, the difference between a protected and package private constructor?
Thanks for your help in advance.
 
Knute Snortum
Sheriff
Posts: 5121
138
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karla Carr,

Please don't start a new thread about the same subject.  Thank you.
 
Saloon Keeper
Posts: 1037
43
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Karla Carr wrote:I have specified in the comments in the code where I have been getting an error in my code.

It's always good to copy paste the complete error message here.

It seems you haven't followed what CR asked you to ...

Could someone please explain me why the instantiation doesn't work?


Because you can't access a protected constructor by a class instance creation expression outside the package in which it is defined.
 
This is what JLS says about protected constructor:

JLS, 6.6.2.2. Access to a protected Constructor wrote:
....
....
A protected constructor can be accessed by a class instance creation expression (that does not declare an anonymous class) or a method reference expression only from within the package in which it is defined.



Also, the difference between a protected and package private constructor?

private constructor is accessed within the body of a class It is defined in. Used in Singleton pattern, to restrict a class instantiation from outside code of that class....

protected constructor is accessed within the class, by classes in same package and sub classes in other packages ( by super(), anonymous class ..)

 
Campbell Ritchie
Marshal
Posts: 60136
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And things package private can only be accessed inside their own package. The official title is default access.
The superclass' constructor is accessible from inside the subclass' constructor with the super(...); construct. That is creating part of the instance. So is use of the superclass' methods in subclass methods, maybe with super.foo(...); That is where protected access is permissible.
On the other hand, writing Superclass s = new Superclass(...); does not constitute part of the instance. That is a completely different instance, so protected access isn't permissible there.
 
author
Sheriff
Posts: 23588
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Karla Carr wrote:

Could someone please explain me why the instantiation doesn't work?



In this case, the error is caused by section 6.6.2 of the JLS... see https://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#jls-6.6.2

Or search this site for "responsible for implementation".

To give full details, line 6 is trying to instantiate an instance that calls a protected constructor in another package. This is not allowed. In line 5, there is a difference. The superclass constructor call is being done for a subclass object -- and it is from a subclass constructor, which is code that is responsible for the implementation (of the subclass instance).

Henry
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!