Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we inherit singleton class?

Can we inherit singleton class?

like image 254
Ram Avatar asked Aug 25 '10 10:08

Ram


2 Answers

It depends on implementation. Singletons usually have private constructor and possibly marked sealed, if it is so then you can't. If it is at least protected you can. If you just inherit from singleton class, result will not be singleton so you should follow the pattern and make it also singleton.

like image 121
Andrey Avatar answered Sep 19 '22 15:09

Andrey


Yes you can. Keep base class constructor protected (and not private).

Then derived class can be instantiated but base class cannot be (even inside function definitions of derived class). I've tried this and it works well.

like image 26
Atul Avatar answered Sep 17 '22 15:09

Atul