My friend bruno started a thread about interfaces and abstract classes and I wanna continue here. I totally agree with his points and I’m adding other ones here. About interfaces:
- define a communication “protocol” between classes.
- isolate the clients from the implementation.
- clients don’t have to concern with implementation details, because they only work with the interface. The real type doesn’t care for the client;
- define the form for a class. More precisely, is what the classes look like while the implementation says how the classes work.
It is, a class which has an interface as a colaborator knows which operations it can send messages for this interface;
So, you are free to change the internal structure of your implementation and the clients don’t have to concern with this. So, we are applying the hiding mechanism here, known as encapsulation;
About abstract classes:
- describe an incomplete abstraction definition, as interfaces do;
- define a common interface for all the subtypes;
- establish a basic form, so it’s easy to say what’s in common between the subtypes;
- express only a interface, not a particular implementation
Interfaces and abstract classes are ways of managing the internal coupling of your code. You create them when you want to manipulate classes through this common interface. If you only want to factor behavior, interfaces are a good choice, but if you want to factor structure too, choose abstract classes.