• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can i declare constructor as protected ???

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic