I know there are other questions that ask about what is favor composition over inheritance. I have read examples that use Animals, Ducks, Lions and Fruits, Vegetables examples but they never seem to sink in for me.
However, what I want to ask here is if the following example is a good example of favor composition over inheritance.
The Example: in the context of Java we have class Thread
and interface Runnable
. If I extend Thread then I can't extend anything else in the future and this might be a problem (making my subclass highly coupled to Thread class). But if I implement interface Runnable I'm leaving my class (that implements Runnable) open to extend any needed superclass in case I need it. This gives more flexibility and makes code less coupled.
Aucun commentaire:
Enregistrer un commentaire