aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes private constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "private constructors" Watch "private constructors" New topic
Author

private constructors

Karthik Veeramani
Ranch Hand

Joined: Dec 22, 2002
Posts: 132
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?).


Thanks<br />Karthik<br />SCJP 1.4, CCNA.<br /> <br />"Success is relative. More the success, more the relatives."
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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.


SCJP Tipline, etc.
Rich Raposa
Ranch Hand

Joined: Dec 06, 2001
Posts: 46
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.


Rich Raposa<br /><a href="http://www.javalicense.com" target="_blank" rel="nofollow">http://www.javalicense.com</a><br />Buy Java courseware -> get a free XBox!
Karthik Veeramani
Ranch Hand

Joined: Dec 22, 2002
Posts: 132
Thanks Rich, thats all I wanted. Probably I didnt word my question properly.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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

Joined: May 11, 2002
Posts: 103
Another good use for a private Constructor is the implementation of the Singleton Pattern.


Gustavo Adolpho Bonesso<br />SCJP1.4
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

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

Joined: Dec 20, 2001
Posts: 3271
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

Joined: Sep 29, 2002
Posts: 1683
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


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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

Joined: Mar 21, 2003
Posts: 19
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

Joined: Dec 06, 2001
Posts: 46
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

Joined: Aug 05, 2002
Posts: 787
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: private constructors