Take 2 classes.i.e., Class A,Class B.In Class B declare constructor as Private.But I have to access Class A using the constructor in Class B.How is it possible?One interviewer told that Through getInstance() method it's possible to access Class A.eEven Class B declare as Private.How is it possible?Explain with an example.

Showing Answers 1 - 2 of 2 Answers

Nithya

  • Nov 29th, 2006
 

it is possible using a  'FRIEND'

  Was this answer useful?  Yes

byteTech

  • Jan 9th, 2007
 

the whole point of making the constructor of Class B private is to prevent its instantiation by the general audience. This is the genesis of factory design pattern, Class B in this case will define a getInstance() method which will return objects of class A

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.