Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

private constructors

 
Karthik Veeramani
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any 1 please explain the behavior of private and protected constructors? I mean, when they'll cause runtime errors (they dont give compile errors, right?).
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Karthik Veeramani:
Can any 1 please explain the behavior of private and protected constructors? I mean, when they'll cause runtime errors (they dont give compile errors, right?).

Why would a private or protected constructor cause a runtime error? Things like null pointers and divide by zero give runtime errors, constructors don't.
 
Rich Raposa
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a constructor is private, then it is only accessible from other constructors within the class (sounds a lot like a private field or method).
If a constructor is protected, then it is only accessible from other classes within the same package, or child classes (sounds a lot like a protected field or method).
Private and protected, as with the default access, have the same effect on constructors as they do on any other member of a class. You'll have a hard time getting a runtime error to occur, because the compiler is going to catch any mis-use of a private or protected constructor.
 
Karthik Veeramani
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rich, thats all I wanted. Probably I didnt word my question properly.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rich Raposa:
If a constructor is protected, then it is only accessible from other classes within the same package, or child classes...

NO! Protected constructors are accessible ONLY from other classes within the package. Child classes outside that package CAN NOT access a protected constructor of the parent class.
 
Gustavo Adolpho Bonesso
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another good use for a private Constructor is the implementation of the Singleton Pattern.
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Corey McGlone:
NO! Protected constructors are accessible ONLY from other classes within the package. Child classes outside that package CAN NOT access a protected constructor of the parent class.

why's that -- isn't the def'n of package that it can only be accessed by other classes in the same package. and the def'n of protected is that it can be acced by other classes in the same package, and by subclasses (even if they're outside the package)??
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jessica Sant:

why's that -- isn't the def'n of package that it can only be accessed by other classes in the same package. and the def'n of protected is that it can be acced by other classes in the same package, and by subclasses (even if they're outside the package)??

Indeed, that's the general idea, but those rules don't apply to constructors. Let me quote the JLS, §6.4.2 The Members of a Class Type

The members of a class type (�8.2) are classes (�8.5, �9.5), interfaces (�8.5, �9.5), fields (�8.3, �9.3, �10.7), and methods (�8.4, �9.4). Members are either declared in the type, or inherited because they are accessible members of a superclass or superinterface which are neither private nor hidden nor overridden (�8.4.6).

...

Constructors (�8.8) are not members.

So, as constructors are not considered members, they are not inherited by subclasses. (Does it really make sense for a class to inherit a constructor, anyway? :confused Therefore, the subclass has no greater access to a protected constructor than any other class. Therefore, if the subclass is outside the package of the superclass, it can not access the protected constructor of the superclass.
Corey
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the JLS says that "A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object."
http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#62587
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if you read on a little further, you find §6.6.2.2 Qualified Access to a protected Constructor, where the JLS states:

If the access is by a superclass constructor invocation super(. . .) ... then the access is permitted.

...

Otherwise, if the access is by a simple class instance creation expression of the form new C(. . .) ... then the access is not permitted.

In other words:

I hope that helps,
Corey
 
Lih Chang
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I understood correctly. But, here is a code snippet that accessing protected constructor in the subclass of another package.
 
Rich Raposa
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Corey McGlone:

NO! Protected constructors are accessible ONLY from other classes within the package. Child classes outside that package CAN NOT access a protected constructor of the parent class.

Maybe this might clarify your statement above: another class in the same package has access to a protected constructor when instantiating the class using "new". A child class has access to a protected constructor when invoking that constructor using "super".
So the question now is: Can a child class instantiate a parent class using "new" on a protected constructor (assuming the child class is in a different package)? I would say no with 99.9% confidence, with a simple example proving my instinct.
Perhaps more importantly: is there ever a good reason for a child class to create an instance of its parent?
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my understanding:
1. All protected members and protected constructors also behave like package members and package constructors within same package.
2. So other classes in same package can access them by direct invocation or via an object of calling class.
3. Child classes in other package can ONLY access protected members and protected constructors via an object of calling class. No direct invocation (by name) of protected members or protected constructor is allowed from a child class if it is in different package.
4. The point that is being made in this thread is not only true of protected constructors but is also true of protected members as well.
Thanks
Barkat
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic