Hi Guys
NP45 public, private & protected
I wish to know only public One() can be called constructor or we can use and call protected One() and private One() as well.
Please anyone knows explain.
Thank you
Hi Guys
NP45 public, private & protected
I wish to know only public One() can be called constructor or we can use and call protected One() and private One() as well.
Please anyone knows explain.
Thank you
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
AlanRank #669 · 3.6K reputationPosted Sep 27, 2007, 8:42 AM EDT
Hi Maha,
Yes, it's possible to have protected, private and internal instance constructors as well as public ones.
A private constructor would be used where you don't want a class to be externally instantiated and you don't want to inherit from it. However, it could still be called via a static method of the same class which is what generally happens if you're creating a 'singleton' class i.e. a class where only one object will ever be created.
A protected constructor would be used where you don't want a class to be externally instantiated but you do want derived class constructors to be able to access it. It is typically confined to 'abstract' classes.
Finally, an internal constructor would be used where you only want a class to be instantiated within the assembly itself.
Posted Sep 27, 2007, 9:21 AM EDT
Thank you very much for your explanation, Alan