I attended an interview and I was asked to design a class for the following requirement. Assume I have a class A and it can have any number of children, i.e., subclasses. The class A has a method called doSomething() which is synchronized. The requirements are :
It is mandatory that all subclasses of A override the doSomething() method.
All subclasses' overriden doSomething() method must be Thread safe in nature.
All subclasses' must have the provision to implement their own logic for their doSomething() method implementations.
Class A's constructor is upto me(the designer) to decide how to implement.
The designer has no control on how many subclasses would be created or how they would be created,i.e., the designer can only write code for the superclass only.
I suggested to make the class abstract and also the doSomething() method abstract. This would imply classes extending my class necessarily provide their own doSomething() method.
However, I could not answer as to what exactly in my class A would ensure Thread safety for my child classes and that too just for the doSomething() method.
He gave a hint though, he said the trick is to be done in A class' constructor.
Any ideas?