Skip to main content
1 of 4
Doc Brown
  • 214k
  • 34
  • 394
  • 603

When in doubt, it is usually better to prefer composition over inheritance. Still, that is just a heuristic, not a law, and asking if this heuristic applies to your specific case is definitely justified.

Here is a simple argument why I think you should prefer composition in this case: if you want to keep your design open to make Drawable a base class of certain more specific "drawables" inside your library (like 2D or 3D drawables, or OpenGL vs. DirectX drawables, or some other kind of technical classification), use composition. Such a technical inheritance hierarchy will not interact well when mixing it with a domain specific inheritance hierarchy outside your library, where ProgramObject may be the base class of certain domain specific class (like a player character, or a vehicle, or some kind of other object). So better aim for two separated inheritance trees.

Doc Brown
  • 214k
  • 34
  • 394
  • 603