This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes Can i declare constructor as protected ??? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Can i declare constructor as protected ???" Watch "Can i declare constructor as protected ???" New topic
Author

Can i declare constructor as protected ???

Waez Ali
Ranch Hand

Joined: Jan 24, 2005
Posts: 42
Hi,
If answer is yes, then please explain how it would be used?
I mean constructors are not inherited in sub classes then what is the use of declaring it as protected.

Thanks.
[ February 28, 2005: Message edited by: Waez ]
Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
One reason you may want to constrain access to constructors is if you want to control creation of the object. An example of this would be the Singleton pattern. If you search google for it, you will find lots of descriptions, along with heated debate about it's usefulness!

I realised that I didn't answer your question directly. A protected constructor is only available to the class and it's subclasses. So, this could be used in a situation where you want the subclass to have control over initialisation, while restricting instantiation of the base type.

E.g.


[ February 28, 2005: Message edited by: Horatio Westock ]
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489


I mean constructors are not inherited in sub classes


wrong. cosntructors are inherited.

ram.
Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
From JLS:

"Constructors, static initializers, and instance initializers are not members and therefore are not inherited."
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489


Constructors, static initializers, and instance initializers are not members and therefore are not inherited.


I stand corrected. Apologies, I was thinking (incorrectly) of super class access

ram.
Waez Ali
Ranch Hand

Joined: Jan 24, 2005
Posts: 42
Hi westok,
I have understood how we can have control over initialization of a class,that really sounds good .
But still I think I need to look for some exapmles that demonstrate its usefullness,so that I could use it in that way.
see If you or somebody can provide it.

Thanks
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
You'd better change your name to comply with site policies before one of the sherrifs comes in.

As to your question, think of singletons and factories.


42
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
An abstract class might declare a protected constructor. This makes it more clear that the abstract class cannot be instantiated, but any subclasses can still call the constructor.

Horatio said, "A protected constructor is only available to the class and it's subclasses." This is not completely true. The protected access modifier also grants access to other classes in the same package, even if they are not subclasses of said class.

HTH

Layne


Java API Documentation
The Java Tutorial
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Can i declare constructor as protected ???
 
Similar Threads
access modifier of interface method
protected methods in java.lang.Object
WA #1.....word association
doGet and doPost
Testing private DAO method