Home > Articles > Programming > General Programming/Other Languages

  • Print
  • + Share This
This chapter is from the book

Instances and Methods

A unique occurrence of a class is an instance, and the actions that are performed on the instance 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 the methods listed in Table 3.1 can be considered instance methods.) Finding out how many types of cars a manufacturer makes would apply to the class, so it would be a class method.

Suppose you have two cars that came off the assembly line and are seemingly identical: They both have the same interior, same paint color, and so on. They might start out the same, but as each car is used by its respective owner, its unique characteristics or properties change. For example, one car might end up with a scratch on it, and the other might have more miles on it. Each instance or object contains not only information about its initial characteristics acquired from the factory but also its current characteristics. Those characteristics can change dynamically. As you drive your car, the gas tank becomes depleted, the car gets dirtier, and the tires get a little more worn.

Applying a method to an object can affect the state of that object. If your method is to “fill up my car with gas,” after that method is performed, your car’s gas tank will be full. The method then will have affected the state of the car’s gas tank.

The key concepts here are that objects are unique representations from a class, and each object contains some information (data) that is typically private to that object. The methods provide the means of accessing and changing that data.

The Objective-C programming language has the following particular syntax for applying methods to classes and instances:

[ ClassOrInstance method ];

In this syntax, a left bracket is followed by the name of a class or instance of that class, which is followed by one or more spaces, which is followed by the method you want to perform. Finally, it is closed off with a right bracket and a terminating semicolon. When you ask a class or an instance to perform some action, you say that you are sending it a message; the recipient of that message is called the receiver. So another way to look at the general format described previously is as follows:

[ receiver message ];

Let’s go back to the previous list and write everything in this new syntax. Before you do that, though, you need to get your new car. Go to the factory for that, like so:

yourCar = [Car new];     get a new car

You send a new message to the Car class (the receiver of the message) asking it to give you a new car. The resulting object (which represents your unique car) is then stored in the variable yourCar. From now on, yourCar can be used to refer to your instance of the car, which you got from the factory.

Because you went to the factory to get the car, the method new is called a factory or class method. The rest of the actions on your new car will be instance methods because they apply to your car. Here are some sample message expressions you might write for your car:

[yourCar prep];         get it ready for first-time use
[yourCar drive];        drive your car
[yourCar wash];         wash your car
[yourCar getGas];       put gas in your car if you need it
[yourCar service];      service your car

[yourCar topDown];      if it's a convertible
[yourCar topUp];
currentMileage = [yourCar odometer];

This last example shows an instance method that returns information—presumably, the current mileage, as indicated on the odometer. Here, we store that information inside a variable in our program called currentMileage.

Here’s an example of where a method takes an argument that specifies a particular value that may differ from one method call to the next:

[yourCar setSpeed: 55];  set the speed to 55 mph

Your sister, Sue, can use the same methods for her own instance of a car:

[suesCar drive];
[suesCar wash];
[suesCar getGas];

Applying the same methods to different objects is one of the key concepts of object-oriented programming, and you’ll learn more about it later.

You probably won’t need to work with cars in your programs. Your objects will likely be computer-oriented things, such as windows, rectangles, pieces of text, or maybe even a calculator or a playlist of songs. And just like the methods used for your cars, your methods might look similar, as in the following:

[myWindow erase];

Clear the window

theArea = [myRect area];

Calculate the area of the rectangle

[userText spellCheck];

Spell-check some text

[deskCalculator clearEntry];

Clear the last entry

[favoritePlaylist showSongs];

Show the songs in a playlist of favorites

[phoneNumber dial];

Dial a phone number

[myTable reloadData];

Show the updated table’s data

n = [aTouch tapCount];

Store the number of times the display was tapped

  • + Share This
  • 🔖 Save To Your Account