1. What Is an Object Anyway?
An object is a thing. Think about object-oriented programming as a thing and something you want to do to that thing. This is in contrast to a programming language such as C, more formally known as a procedural programming language. In C, you typically think about what you want to do first (and maybe write some functions to accomplish those tasks), and then you worry about the objects—almost the opposite from object orienta- tion.
As an example from everyday life, assume you own a car. That car is obviously an object—one that you own.You don’t have just any car; you have a particular car that was manufactured from the factory, perhaps in Detroit, perhaps in Japan, or perhaps some- place else.Your car has a vehicle identification number (VIN) that uniquely identifies your car.
In object-oriented parlance, your car is an instance of a car. And continuing with the terminology, car is the name of the class from which this instance was created. So, each time a new car gets manufactured, a new instance from the class of cars gets created. Each instance of the car is also referred to as an object.
Now, your car might be silver, it might have a black interior, it might be a convertible or hardtop, and so on. In addition, there are certain things, or actions, you do with your car. For example, you drive your car, you fill it up with gas, you (hopefully) wash your car, you take it in for service, and so on. This is depicted in Table 19.1.
Table 19.1 Actions on Objects
Object What You Do with It
Your car Drive it
Fill it with gas
Wash it
Service it
The actions listed in Table 19.1 can be done with your car, and they can also be done with other cars. For example, your sister can drive her car, wash it, fill it up with gas, and so on.
2. Instances and Methods
A unique occurrence of a class is an instance. The actions that you perform are called methods. In some cases, a method can be applied to an instance of the class or to the class itself. For example, washing your car applies to an instance (in fact, all of the methods listed in Table 19.1 are considered instance methods). Finding out how many different types of cars a manufacturer makes applies to the class, so it is a class method.
In C++, you invoke a method on an instance using the following notation:
Instance.method ();
A C# method is invoked with the same notation as follows:
Instance.method ();
An Objective-C message call follows this format:
[Instance method]
Go back to the previous list and write a message expression in this new syntax. Assume that yourCar is an object from the Car class. Table 19.2 shows what message expressions might look like in the three OOP languages.
And if your sister has a car, called suesCar, for example, then she can invoke the same methods on her car, as follows:
suesCar.drive() suesCar.drive() [suesCar drive]
This is one of the key concepts behind object-oriented programming (that is, applying the same methods to different objects).
Another key concept, known as polymorphism, allows you to send the same message to instances from different classes. For example, if you have a Boat class, and an instance from that class called myBoat, then polymorphism allows you to write the following message expressions in C++:
myBoat.service()
myBoat.wash()
The key here is that you can write a method for the Boat class that knows about servic- ing a boat, that can be (and probably is) completely different from the method in the Car class that knows how to service a car. This is the key to polymorphism.
The important distinction for you to understand about OOP languages versus C, is that in the former case you are working with objects, such as cars and boats. In the latter, you are typically working with functions (or procedures). In a so-called procedural lan- guage like C, you might write a function called service and then inside that function write separate code to handle servicing different vehicles, such as cars, boats, or bicycles.
If you ever want to add a new type of vehicle, you have to modify all functions that deal with different vehicle types. In the case of an OOP language, you just define a new class for that vehicle and add new methods to that class.You don’t have to worry about the other vehicle classes; they are independent of your class, so you don’t have to modify their code (to which you might not even have access).
The classes you work with in your OOP programs will probably not be cars or boats. More likely, they’ll be objects such as windows, rectangles, clipboards, and so on. The messages you’ll send (in a language like C#) will look like this:
myWindow.erase() Erase the window
myRect.getArea() Calculate the area of the rectangle
userText.spellCheck() Spell check some text
deskCalculator.setAccumulator(0.0) Clear the accumulator
favoritePlaylist.showSongs() Show songs in favorite playlist
Source: Kochan Stephen G. (2004), Programming in C: A Complete Introduction to the C Programming Language, Sams; Subsequent edition.