Suppose I have a POJO A and some client code that serves it. I have created POJO B, which doesn't have anything in common with A, but I want to use it in the methods that use A, so I decided to create a marker interface C, that both implement and change the signature of the methods to serve them both.

Considering that apart from being able to be served by those methods, these two POJO don't have one field or method in common, should they really be considered as part of some interface, that I understand defines a contract or behaviour? Since this "contract C" does not specify any behaviour is it a contract at all? How does Liskov Substitution Principle define this? You could say that both POJOs can be replaced by C in these methods, meaning nothing breaks, so it sounds like the rule is not broken at all.

  • 3
    If A and B have no members in common at all, then what does the client code actually do to them? Does it use only Object methods? Then it should simply accept Object and not EitherAOrB. – Kilian Foth Apr 26 '16 at 16:57
  • If you can modify both classes A and B, why don't you extract a real interface? You have to, to replace the one with the other. I think it would be easier to give an answer if you were more concrete about A and B. – Andy Apr 26 '16 at 17:49

I would say that Marker interfaces are a code smell in themselves. I'm not convinced they were ever a good idea but annotations definitely make them obsolete.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.