DEV Community

Cover image for Code Smell 11 - Subclassification for Code Reuse

Code Smell 11 - Subclassification for Code Reuse

Maxi Contieri on October 30, 2020

Code reuse is good. But subclassing generates a static coupling. TL;DR: Favor composition over inheritance. Always. Period. Problems ...
Collapse
 
pyrsmk profile image
Aurélien Delogu

I don't know well Java but isn't there interfaces in this language? Because extending abstract classes is still inheritance IMO, even if you can use them as interfaces. In Ruby we only have abstract classes too, so I can understand ahah.

In all cases, your Box class is an anti composition pattern. It should encapsulate an instance of Square instead of creating it in its constructor. It makes the code less testable ;)