Section 1.8: Object-Oriented Programming
Some things are data and behavior together, and splitting them apart makes both worse. A class lets you define what a kind of thing holds and what it can do in one place, then create as many independent instances of it as you need. This section covers that definition, the difference between data belonging to one instance and data shared by all of them, the boundary between what a user of your class may touch and what is internal, and the two ways one class builds on another, inheriting from it or containing it.
Section 7 let you package your code so it runs on more than your own machine. What it did not settle is what that package should expose. Functions passing dictionaries to other functions works, and it works less well every time the dictionary gains a key that only some functions know about. Every library you will use from Module 2 onward hands you objects with methods, so this is also the section that makes the rest of the bootcamp readable.
Lessons
- Classes, Objects, and Methods - defining a type and creating instances of it.
- Instance vs Class Data, Representation, and Encapsulation - what belongs to one object against what is shared, and what to keep internal.
- Inheritance, Overriding, and Composition - building on an existing class, replacing part of its behavior, and the alternative of containing one instead.
Where this section is exercised: the module capstone, The Run Registry, your own leaderboard of training runs, built from nothing but the standard library. It leans hardest on this section.
By the end you will be able to:
- Define a class with an initializer and methods, and create independent instances of it.
- Distinguish instance data from class data, and predict what happens when either is mutated.
- Override an inherited method and call the original from the replacement.
- Choose between inheritance and composition for a stated relationship, and justify it.