News for hackers. Stuff that matters.
Re:When did people favor composition over inheritance? by grahamlee on Sunday November 16, 2025 @06:21AM (#45944232)
Author here. I wrote “ But even a modestly more recent language like Java has visibility attributes that let a class control what its subtypes can view or change, meaning that any modification in a subclass can be designed before we even know that a subtype is needed.” which covers your situation: if you need to ensure that subtypes use the supertype’s behaviour in limited ways, use the visibility modifiers and `final` modifier to impose those limits.
Re:When did people favor composition over inheritance? by bccdee on Sunday November 16, 2025 @04:42PM (#45948676)
The fact that Java had to add a whole extra set of keywords to control this indicates that this is a site of complexity. Since it isn't needed for composition, it's a site of unnecessary complexity.
Re:When did people favor composition over inheritance? by grahamlee on Monday November 17, 2025 @12:57PM (#45956083)
What you lose by using composition is that the composing object is no longer a subtype of the constituent object, so you can't use it as a "decoration" of the original object in a program that expects an instance of the original.
Re:When did people favor composition over inheritance? by bccdee on Monday November 17, 2025 @05:36PM (#45959200)
It can be, if the composing object re-implements the constituent object's interface. This way, code reuse and polymorphism are orthogonal features, which I think is better. If you want both, you can do both, but inheritance pushes you toward using both even when you only need one.