Java – When shall we go for interface or abstract class in Java?

java

Possible Duplicate:
When to use an interface instead of an abstract class and vice versa?

Can any one tell me under which circumstances we should go for interface and abstract class.

Java specific aspects are welcome.

Best Answer

Always use an interface unless you need to ...

  • ... provide subclasses with some state
  • ... provide subclasses with default implementations of some methods
  • ... want to defer implementation of some of the abstract methods

Note that you can only extend one class, while you can implement multiple interfaces. So if there's any chance that a subclass will need to extend some other class, strive for using an interface.

Here are some good links that discusses this topic: