The Car Example for OOP

The `Car extends Vehicle` or `Duck extends Bird` type of tutorial obscures more than it illuminates. In good OO programming, we don’t make class hierarchies in order to satisfy our inner Linnaeus. We make class hierarchies in order to simplify the code by allowing different parts of it to be changed independently of each other, and to eliminate duplication (which comes to the same thing). Without any context as to what the code needs to accomplish, you can’t make a judgment about whether a particular design decision is good or bad.

The problem with examples like `Duck extends Bird` is that it gives you no understanding of the kind of considerations you need to think about in order to decide whether the design decisions discussed above are good or bad.

In fact, it actively sabotages that understanding.

You can’t add code to ducks.

You can’t refactor ducks.

Ducks don’t implement protocols.

You can’t create a new species in order to separate some concerns (e.g. file I/O and word splitting).

You can’t fake the ability to turn a duck into a penguin by moving its duckness into an animal of some other species that can be replaced at runtime.

You can’t indirect the creation of ducks through a factory that produces birds of several species, and even if you could, the analogy doesn’t help at all in understanding why the analogous thing might be a good idea in an actual program.

Penguins don’t implement the “fly” method that can be found in birds.

Whether you consider ducks to be birds or simply chordates does not affect the internal complexity of ducks.

And you don’t go around causing things to fly without knowing what kind of bird they are. (Ducks themselves decide when they want to fly, and they certainly seem to know they’re ducks and not vultures.)

So, although some people claim that such analogies “make it easier to grok what polymorphism is about”, I disagree. It’s misleading; it obscures the relevant while confusing people with the irrelevant.

Notes:

Folksonomies: education computer science

Taxonomies:
/pets/birds (0.996253)

Concepts:
Bird (0.978107): dbpedia_resource
Object-oriented programming (0.792608): dbpedia_resource
Analogy (0.762535): dbpedia_resource
English-language films (0.720537): dbpedia_resource
Penguin (0.676085): dbpedia_resource
Duck (0.673542): dbpedia_resource
Class (0.634826): dbpedia_resource
Abstraction (0.612851): dbpedia_resource

 Goodbye, shitty Car extends Vehicle object-orientation tutorial
Electronic/World Wide Web>Internet Article:  Sitaker, Kragen Javier (2011-08-22), Goodbye, shitty Car extends Vehicle object-orientation tutorial, Retrieved on 2022-06-24
  • Source Material [www.mail-archive.com]
  • Folksonomies: education programming