Kayavak / The Backend / BE·34 Object-Oriented Programming and Its Alternatives / Deeper
Deeper · The Backend
Object-Oriented Programming and Its Alternatives
The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.
Why does this exist?
Object-oriented programming began with simulation. Simula 67 (Ole-Johan Dahl and Kristen Nygaard, Norway) modelled ships and queues as objects carrying their own data and behaviour, with classes and inheritance. Alan Kay's team at Xerox PARC built Smalltalk in the 1970s around objects sending messages, and Kay has said the messaging, not the classes, was the important idea. C++ (Bjarne Stroustrup, from 1979) and then Java (1995) made classes mainstream for large commercial software.
Experience refined the practice. Barbara Liskov's 1987 keynote, and her 1994 paper with Jeannette Wing, defined when a subtype can safely stand in for its parent: the Liskov substitution principle. The "Gang of Four" Design Patterns book (1994) urged "favour object composition over class inheritance" after a decade of fragile hierarchies. Today's alternatives (records and sealed types, algebraic data types, functional core with an imperative shell) keep the parts that protect invariants and drop the parts that tangled code.
The mental model
Try it yourself first: write your own model in the Mental Model Explorer, then compare.
An object is a guard around some state: the only way to change it is through methods that keep it valid. Everything else in OOP (interfaces, polymorphism, inheritance) is a way to let code depend on what an object can do rather than how it does it.
| Question | Answer |
|---|---|
| What is it? | Bundling state with the operations allowed to change it, and programming against capabilities |
| Problem it solves | Keeping invariants true and managing change in large codebases with many authors |
| Parts | Encapsulation, interfaces, dynamic dispatch, inheritance, composition, value objects |
| How it works | Private fields, public methods that check and preserve invariants; callers see interfaces; the runtime picks the implementation |
| Assumptions | Subtypes honour their parent's contract (LSP) |
| When they change | A subclass that breaks the contract (a square that can't change width alone) breaks every caller that trusted the parent |
| Where it fails | Deep inheritance hierarchies; anaemic objects with all logic elsewhere; modelling data as behaviour when it's just data |
| Related ideas | Abstraction and interfaces (Foundations, ch02); dynamic dispatch's cost (ch40); architecture (Software Architecture) |
What if?
What if every field were public?
Reason it through, then open
Any code anywhere could put an object into an invalid state (a negative balance, an order with no items), so every reader would have to re-check it, and any change to the representation would break callers you can't find. Encapsulation makes the class the single place that has to get the rules right.
What if you reused code only through inheritance?
Reason it through, then open
Every class would carry its parents' implementation details, and a change to a base class could silently break subclasses that relied on how it called its own methods: the fragile base class problem. Composition (holding an object and delegating to it) reuses behaviour through its public contract only, so the parts can change independently.
Where the boundary is
| Aspect | Inheritance | Composition |
|---|---|---|
| Relationship | Is-a | Has-a, uses-a |
| Reuses | Implementation | Behaviour through an interface |
| Coupling | Tight: subclasses see protected details | Loose: only the public contract |
| Change at run time | No | Yes: swap the delegate |
| Use when | A true subtype that honours the contract | Almost everything else |
How sure are we?
| Claim | How it's known | Where from |
|---|---|---|
| Simula 67 introduced classes and inheritance | Historical | Dahl and Nygaard's work, 1967 |
| Substitutability defines safe subtyping | Theorem, under its definitions | Liskov and Wing, 1994 |
| Composition is usually better than inheritance | Model | Widely accepted design advice (Gang of Four, 1994); not universal |
Still open
How much object orientation is right is argued constantly: data-oriented design for performance, functional styles for correctness, and object models for domain logic each have strong advocates. Modern Java uses all three.
What would change your mind? If large codebases built mainly with inheritance hierarchies stayed easy to change, the case for composition would weaken. Experience mostly reports the opposite.
Recall
What does the Liskov substitution principle require?
A subtype must be usable anywhere its parent is, honouring the parent's contract.
What's the fragile base class problem?
Changes to a base class's internals silently break subclasses that depend on them.
What is encapsulation for?
Making one class responsible for keeping its state valid, so no other code can break its invariants.
When is inheritance appropriate?
For a genuine subtype that fully honours the parent's contract.
Reflect
Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.