jargon

Comparison

Composition over inheritancevsInheritance

Composition over inheritance

the subclass needed one method overridden and inherited four others it never wanted and cannot remove.

Preferring to build behaviour by combining objects rather than by extending a base class. Inheritance couples you to everything the parent does now and everything it will do later, and it is a decision you make once at compile time; composition is a decision you can change per instance. The argument comes up whenever a class hierarchy has grown a third level, which is usually where the abstraction that justified the first level stops being true.

Full entry →

Inheritance

you got twenty methods you never asked for along with the one you wanted, and you cannot give any of them back.

Deriving a class from another so it acquires the parent's fields and methods and may replace some of them. It is the strongest coupling one class can have to another: the subclass depends not just on the parent's public surface but on which of its own methods the parent calls internally. That is why it is the default in textbooks and increasingly the second choice in practice.

Full entry →

Related comparisons