jargon

Comparison

InheritancevsIs-a vs has-a

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 →

Is-a vs has-a

you said it out loud — a Stack is a List — and it sounded fine right up until someone called insert in the middle of it.

The rough test for whether a relationship should be inheritance or a field: inheritance claims the subtype is usable everywhere the supertype is, while composition only claims it uses one. It is rough because the English sentence is easy to talk yourself into; the real test is whether every operation the parent promises still makes sense. Most classic inheritance mistakes pass the sentence and fail the operation.

Full entry →

Related comparisons