Understanding Classes and Objects
You can never have too many analogies to describe object-oriented programming. This article doesn’t define the terms class and object — or even explain the terms class and object. Instead, it offers several analogies to help you visualize classes and objects. This supplements your reading in more formal treatments of the concepts.
None of these analogies are flawless. Each analogy has weaknesses. But, taken together, the analogies help you get a better understanding of what it means to be a class or an object.
A class is like a blueprint for a kind of house in a housing development. An object is like a particular house. The blueprint says things like “Each house’s living room has its own color paint.” A particular house has red paint, or white paint, or some other color paint.
A class is like the headings of a table. An object is like a row in the table. For example, a table to describe a bank account might have headings such as AccountHolderName, Balance, and SavingsOrChecking. These headings are the class’s fields. A particular object (a row in the table) might have field values “Barry Burd” (the account holder’s name), $24.60 (the account balance), and Checking.
If you read Plato in your spare time, you know about forms (also known as ideals). A form is an abstraction of a certain type of thing. For example, if you say the word “chair,” everyone knows what you’re talking about even if they are standing in an empty room with no chairs. When you hear the word “chair,” you might visualize a particular chair, but you’re not surprised when you find out that the chair you have in mind is a bit different than someone else’s. A class is like one of Plato’s forms. In the same way, an object is like a particular thing. An instance of the Chair class (that is, an object constructed from the Chair class) might be made of wood, have a straight back that’s roughly rectangular shaped, might be lacquered brown, and so on. One of the Chair class’s fields is numberOfLegs. Most people imagine chairs with four legs. For most chair objects, the value of numberOfLegs is 4. But you could find someone with a set of collectable three-legged chairs. For each collectable chair objects, the value in the numberOfLegs field is 3.
A class is a description of a kind of thing. An object is an actual thing.