Originally posted by amit taneja:
in one of the interveiw i attended...
i have been asked can constructor have pvt access modifier.. ? and can we create class ?
i said yes we can have pvt constructor we can also create instance of that class which has pvt constructor but only in that class it self... i.e in the code of the same classs
is i m right...i think...
but my question is can we also create class if we instantiate that class in other class..
i know that class can not be inherited but can we instatiate that class as a associtation.. in other class ?
regards
If all the constructors are private, then no, you cannot instantiate that class in another class. However, as the example above illustrates, you can use a factory method to create new instances. This is commonly seen in the Singleton
pattern in order to ensure that only one instance of a class is available. If you want more information,
you should google or search this message board for "Singleton". I think there is something in the FAQ about it, too.
Layne