• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can i declare constructor as protected ???  RSS feed

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I mean constructors are not inherited in sub classes


wrong. cosntructors are inherited.

ram.
 
Horatio Westock
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From JLS:

"Constructors, static initializers, and instance initializers are not members and therefore are not inherited."
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!