 Let's start discussing object-oriented programming with something everybody understands. Toasters. In order to think in an object-oriented way, you need to think about the things an object has, its properties, and the things an object does, its methods. Usually, we start out with properties. What does a toaster have? It has slots, two, three, or four. It has a voltage, 110 or 220, a number of slices of bread currently in the toaster, from zero to the number of slots, an on-off switch, and a darkness setting, usually from one to 10, where one is death warmed over, and 10 is cremation. And here's the corresponding UML diagram for the properties. In terms of methods, we have a constructor. The only two things we need to set ourselves are the number of slots and the voltage. We ship toasters with no bread in them, turned off with the darkness set to one. We'll omit the getters and setters here as there's nothing particularly new in them. What other things does a toaster do? You can turn it on or turn it off. That's a shorthand for set turned on to true or false. You can insert bread. The parameter is a number of slices to insert. And you can pop the bread out, which sets the number of slices to zero. And you can convert the toaster object to a string for display for the users. And here's the code with the properties, the constructor, the getters and setters. And notice, by the way, that when I set the number of slots, I make sure that it stays in the range one to four. And I make sure that the voltage is either 110 or 220. And then the extra methods that I talked about earlier, turn on, turn off, insert bread, and pop bread. And here's two string. Here's some test code. I'm going to create a two-slot toaster that has two slots at 110 volts and a toaster for Europe with four slots and 220 volts. I'll turn on the two-slot toaster, set its darkness control to four, and insert one slice of bread. And then I'll print out both of them. Let's see what that looks like. And there's my output. That's all well and good. But in reality, no manufacturer builds the entire toaster themselves. Instead, they buy the power supply and the dials from some other manufacturer and then put them into the toaster frame, which they themselves build. In an object-oriented world, we use what the book calls composition or aggregation to make classes or objects that are composed of other classes and objects. We'll create a power supply and a dial class. The power supply has the voltage, and whether it's turned on or not. And it does the turning on and turning off. The dial has a setting, and we're adding a minimum and maximum value to make it more generally useful. For example, some companies might want to buy a dial that goes from only one to four instead of one to 10. Our toaster object will now consist of a number of slots and the number of slices of bread. And the power supply object will take care of the voltage and turning on and off. And the dial object will take care of the darkness setting. The only methods we now have for the toaster are the ones that involve the parts that the toaster company is responsible for, inserting the bread and popping it out. In the code, here's our power supply object with the voltage and its on or off status. The constructor, the getter and setter, and the turn on and turn off methods. Here's the dial class, which has its properties, constructor, and the getters and setters. In the toaster, power now becomes the power supply object and darkness becomes a dial object. In the constructor, we must then create a new power supply of the desired voltage and create a new dial for the darkness. When we insert bread, we must first check to see if the toaster is turned on. We can't say get turned on because that method doesn't belong to the toaster. It belongs to the power supply, which is why we need to say power.get turned on. Similarly, in two-string, we need to get the voltage from the power supply and we need to get the setting from the dial in order to be able to use them. In main, we have an expression like this. Red from left to right, we're telling the two-slot toaster to get the power supply object and tell it to turn on. Similarly, in this line, we're telling the two-slot toaster to get its darkness object, the dial, and set its setting to four. You will see this sort of chain of method calls whenever you have objects composed of other objects. In chapter 11, we'll learn another way of adding functionality to objects, inheritance. While this book and many others strongly emphasize inheritance, current best practice serves to prefer composition over inheritance.