Interview Question #1

So, as I mentioned before, I am going to post questions I think will be useful in interviewing applicants to a developer position in an average software development company. Here goes the first one.

Question

We see a class named AbstractShape with a method redraw() on it, with a given behavior already implemented. One of its subclasses, Circle, overrides this method and instead of coding a behavior of its own, throws an UnsupportedOperationException. What is the matter with this design? How can it be fixed?

Answer

So, to answer the question, it would be it is wrong to do that because in Object-oriented Design you should never subtract from functionality provided by a superclass. Now, the person designing that class probably was too lazy to branch a whole new level of hierarchy for this subclasses that explicitly needed the redraw functionality and implement it there, but it just isn’t right.

To correct it, we would have to extend AbstractShape into, say, AbstractRedrawableShape and move that redraw() method into that class. This way, classes that do support being redrawn will have the functionality, while we are not exposing the API for a class that simply will not support it.

Why the question?

Now, why would we ask an applicant this question. This question highlights several general areas of modern programming:

  • Object-oriented design principles and best practices
  • Understanding inheritance
  • The ability to correct a design mistake by pinpointing the problem and providing a meaningful solution.

Leave a Reply

Your email address will not be published. Required fields are marked *