object-oriented programming (intro)

data before action

The object-oriented paradigm inverts the thinking of the procedural paradigm: rather than decompose our problem into units of action (functions), we decompose it into data types. A data type bundles together not just data but also the operations which can be performed upon that type. For example, a cat data type might contain not just a name (e.g. ‘mittens’) and a number of lives left (e.g. 9) but also operations for eating, sleeping, and scratching, because those are all things a cat may do.


The principle of encapsulation says that the data of a data type should only be read and written by operations of that data type, not other types. In effect, a data type should be interacted with only by invoking its operations, not by touching its data.


When data type X inherits from data type Y, X automatically includes all the same data and operations as Y in addition to anything X includes itself. Effectively, X is just like Y but more specific. For example, a cat is a more specific kind of mammal, so it would make sense for a cat data type to inherit from a mammal data type, such that cat inherits operations for eating, sleeping, and giving live birth but then adds its own for, say, purring (because that’s something cats do, not all mammals).

Comments are closed.