Another Way to Understand Object Orientation - Python
Python is a very object-oriented language, which makes it easy to work with. For those of you who may not be familiar with the concept of object-oriented programming languages, Peyton provides a complete, easy-to-understand explanation, and then gives a number of examples that illustrate how object orientation works in Python.
If you still don't understand the concept, let's take a look at your computer. Your computer has its own attributes and methods. For example, if we want to know the clock speed of the computer's processor, we could use access an attribute named clockSpeed:
yourComputer.clockSpeed
We can also overclock the computer by increasing the multiplier by calling a method called modifyMultiplier:
yourComputer.modifyMultiplier()
This would increase the computer's multiplier attribute and increase the clockSpeed attribute. As you can see, the methods of an object perform actions on its data.
Now let's say you bought your computer from a manufacturer named Shiny Silver Computers, Inc. Let's say that your computer is a model named "Basic Computer." "Basic Computer" might be a class. What is a class? A class is basically an outline of the objects within the class. You can compare it to the blueprints of a house. The blueprints would be a class, and the actual house would be an object based on the class. While an object is always very similar to the class on which it is based, its data can vary. For example, you might have requested hat your computer have a blue case with a window.
Or, to use a more technical definition: in object-oriented programming, a class is a template definition of the methods and variables in a particular kind of object. Thus, an object is a specific instance of a class; it contains real values instead of variables.
Now let's say that Shiny Silver Computers, Inc. decides to design a new model based on their "Basic Computer" model. They would subclass the original class. The new class, or subclass, would be based upon the old class, or superclass, but its methods or data might vary, or it might add new methods or data. For example, let's say that the new computer model has a faster clock speed. The clockSpeed attribute would be different. Let's also say that the new model has a button that will turn on lights inside the case. There would be a new method called turnOnLights.
Objects may also interact with each other and message each other. Let's create a screwdriver object. You might want to unscrew a certain screw in your computer, so you would have the screwdriver object call the unscrew method of your computer. The screwdriver object would also pass an argument specifying which screw needs to be unscrewed.
Likewise, you yourself might be an object. You can push the computer's power button by calling its pushPowerButton method. Your computer might then change its powerState variable.
Object orientation makes programming a lot more simple, and it also organizes things. In Python, you will use and manipulate objects regularly.