• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Interview question on Singleton Class

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was asked this question in one interview

1. Since Singleton class has private constructor, can the class be extended?
my answer: Since the constructor is private, it can not be extended

Follow up question

2. Since it can not be extended is it a final class?
my answer: yes, it is a final class.

From their expressions I felt that the answers were wrong.

What do you think?

Thanks
Jeevan.
 
Marshal
Posts: 80280
432
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have a class which cannot be extended, eg because of a private constructor, it would be a good idea to add the keyword final to the class header.

Maybe this thread would sit better on Beginning Java or Java in General??
 
Jeevan Reddy
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Campbell for the inputs.
I posted here since it was job interview question. If you think it's not appropriate forum, please move it.

Thanks
Jeevan.
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That questions seems tricky. You can throw it back to the interviewer and ask 'By final do you mean the class cannot be extended or that the class itself is a "final" class'
 
Jeevan Reddy
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepak Bala wrote:That questions seems tricky. You can throw it back to the interviewer and ask 'By final do you mean the class cannot be extended or that the class itself is a "final" class'



Please tell me what you think in either case (when class is final and when class can not be extended).

Thanks
 
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Strictly speaking, non-final classes with private constructors can be extended using inner classes inside them (provided you're allowed to make changes to the source code):

 
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<post created in error>
 
Ranch Hand
Posts: 8946
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You dont need to have private constructor for singelton. you can have it as protected. refer gof book. Making constrcutor protected you can subclass.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You dont need to have private constructor for singelton. you can have it as protected. refer gof book. Making constrcutor protected you can subclass.




In that case don't you think if the dependent class is in same package as singleton class then there will be no Singletoness functionality.
The dependent class(Non subclass of singleton class) can create the multiple objects of singleton class using new operator.

as far as interview question is concerned...Inner class can only use the private constructor so its not necessary to use final keyword for singleton class..

 
Pradeep bhatt
Ranch Hand
Posts: 8946
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

tarun k navlani wrote:

You dont need to have private constructor for singelton. you can have it as protected. refer gof book. Making constrcutor protected you can subclass.




In that case don't you think if the dependent class is in same package as singleton class then there will be no Singletoness functionality.
The dependent class(Non subclass of singleton class) can create the multiple objects of singleton class using new operator.

as far as interview question is concerned...Inner class can only use the private constructor so its not necessary to use final keyword for singleton class..



You can read GOF book for more details. Making constructor protected gives a chance to override the functionality.
 
And then the entire population worshiped me like unto a god. Well, me and this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic