interface vs abstract class

I’ll keep it simple…

Interfaces should be seen as a contract, they define some method signatures which need to be implemented by any implementing class. Use Interfaces for code that can change easily.

A great example of how to use interfaces is the Strategy Pattern (PDF). Follow the link for a detailed description and example. In general the strategy pattern provides an easy way to implement different strategies (logics) for achieving your goal. On top there is an interface.

Another example of interfaces are the collections In fact it’s a good practice to use these interfaces in your method signatures, this improves the flexibility of your code. You can then easily switch collection implementation. Example:

private List list = new ArrayList();

public List getList(){
return list;
public void setList(List list){
this.list = list;

Abstract classes can also oblige an extending class to implement some methods. The difference is that an abstract class can do more. You can add completed methods, fields, … So an abstract class can provide general implementation, defaults and a contract for some other methods of which the logic will be defined in subclasses.

Don’t forget!

Leave a Reply

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

Please reload

Please Wait