Composition Vs Inheritance

Inheritance should only be used when:

  1. Both classes are in the same logical domain

  2. The subclass is a proper subtype of the superclass

  3. The superclass’s implementation is necessary or appropriate for the subclass

  4. The enhancements made by the subclass are primarily additive.

There are times when all of these things converge:

  1. Higher-level domain modeling

  2. Frameworks and framework extensions

  3. Differential programming

If you’re not doing any of these things, you probably won’t need class inheritance very often. The “preference” for composition is not a matter of “better”, it’s a question of “most appropriate” for your needs, in a specific context.