4.2. Classes, Objects, Methods and Instance VariablesWe will begin with a simple analogy to help you understand classes and their contents. Suppose you want to drive a car and make it go faster by pressing down on its accelerator pedal. What must happen before you can do this? Well, before you can drive a car, someone has to design the car. A car typically begins as engineering drawings, similar to the blueprints used to design a house. These engineering drawings include the design for an accelerator pedal to make the car go faster. The pedal "hides" the complex mechanisms that actually make the car go faster, just as the brake pedal "hides" the mechanisms that slow the car and the steering wheel "hides" the mechanisms that turn the car. This enables people with little or no knowledge of how engines work to easily drive a car. Unfortunately, you cannot drive a car's engineering drawings. Before you can drive a car, the car must be built from the engineering drawings that describe it. A completed car has an actual accelerator pedal to make it go faster, but even that is not enoughthe car will not accelerate on its own, so the driver must press the accelerator pedal. Now let's use our car example to introduce the key programming concepts of this section. Performing a task in a program requires a method. The method describes the mechanisms that actually perform its tasks. The method hides these mechanisms from its user, just as the accelerator pedal of a car hides from the driver the complex mechanisms that make the car go faster. In Visual Basic, we begin by creating a program unit called a class to house a method, just as a car's engineering drawings house the design of an accelerator pedal. In a class, you provide one or more methods that are designed to perform the class's tasks. For example, a class that represents a bank account might contain many methods, including one to deposit money in the account, another to withdraw money from the account and a third to inquire what the current balance is. Just as you cannot drive an engineering drawing of a car, you cannot "drive" a class. Just as someone has to build a car from its engineering drawings before you can actually drive it, you must build an object of a class before you can get a program to perform the tasks the class describes how to do. That is one reason Visual Basic is known as an object-oriented programming language. When you drive a car, pressing its gas pedal sends a message to the car to perform a taskthat is, to go faster. Similarly, you send messages to an objecteach message is a method call and tells one of an object's methods to perform its task. Thus far, we have used the car analogy to introduce classes, objects and methods. In addition to the capabilities a car provides, it also has many attributes, such as its color, the number of doors, the amount of gas in its tank, its current speed and its odometer reading (i.e., its total miles driven since it was built). Like the car's capabilities, its attributes are represented as part of a car's design in its engineering diagrams. As you drive a car, these attributes are always associated with the car. For example, each car knows how much gas is in its own gas tank, but not how much is in the tanks of other cars. Similarly, an object has attributes that are carried with it as it's used in a program. These attributes are specified in the object's class. For example, a bank account object has a balance attribute that represents the amount of money in the account. Each bank account object knows the balance in the account it represents, but not the balances of the other accounts in the bank. Attributes are specified by the class's instance variables. This chapter presents five simple examples that demonstrate the concepts introduced in the context of the car analogy. The examples incrementally build a GradeBook class:
The GradeBook examples in this chapter do not process or store grades. We begin processing grades with the version of class GradeBook in Chapter 5, Control Statements: Part 1, and we store grades in the version of GradeBook in Chapter 8, Arrays. |