Objects and Classes

This articles is based on iOS 5 Objective C 2.0
Programming for iOS is done in Objective C. Objective C, like a lot of programming languages is an Object Oriented Language (Object Oriented Programming – OOP for short). This means that everything you code in Objective C will be manipulating, storing and creating objects. This article aims to explain the theory behind Objects, Classes and associated vocabulary.

Objects Come From Classes

To explain objects and classes, imagine a Car. Before a car is built, there is a plan all drawn out as to what the car is going to be able to do, how it looks, what type of car it is etc. Lets look further at this example.

A car plan may contain information such as length, width, number of doors, transmission type, colour, fuel type and manufacture. These are all but a few attributes of a car.

So every car has these attributes. They are properties of a car. Not every car has the same values for these properties (one car may be red, another green), but all cars have these properties.

A car plan may contain tasks such as start, stop, unlock, lock. All of which are examples of things a car can do.

So every car has these tasks. They are pieces of logic that can be run. For example, the start task involves lighting the ignition, then getting the engine to start and rev over. All cars start in roughly the same way. Lets call these pieces of logic methods.

Imagine then, if you will, that the car plan is called the class. And every car you create from this class is called an object. This is the basics of how objects and classes work. The designers design the car plan (the class), and then the factory makes cars (objects) from the plan (class). Lets call the process of creating a car from the plan (object from the class) instantiation.

So if you have followed along you now know that objects are instantiated from classes. The class sets out what the object is capable of doing, and what properties can be set on the object.

Superclasses and Subclasses

A car, a truck, a motorbike, a scooter, boat, plane, train, bus, tram. What do they all have in common? (This is a question you learn to ask yourself when coding Classes). They are all vehicles. They all get you from A to B, they are all able to perform the task “move forward” and “move backwards” and “start engine”. So we could say that when they draw up a plan for a car, they integrate the plan for a vehicle. This is also true for classes. When you define the car class, you can say that it inherits from the vehicle class. Vehicle is said to be the superclass of Car. And Car a subclass of Vehicle. This means you could create a vehicle object from the vehicle class and it would have the properties and methods defined in the vehicle class. You could also create a car object from the car class and it would have the properties and methods defined in both the car class and the vehicle class.

This serves to be very handy for programmers. It allows us to group common functionality together into superclasses and inherit from them in our custom classes. Subclassing is a technique used in iOS development to customise the functionality of objects.

Variables: Both Instance and Local

When you create an object from a class, you store the object in what is known as a variable. Imagine the variable a box, that is filled with an object. But the variable is shaped like the object so that only a certain type of object can fit inside. For example a Car object would go in a Car box, not a Bike box. This will become more clear when we look at Objective C examples.

Instance Variables are variables that are stored against an object, for example they may hold the values set against object properties. We will get to this shortly.

Local Variables are variables that are created, manipulated and destroyed all within one method. This is handy for calculations or code logic.

Class and Instance Methods

An instance method is a piece of code that is executed against an object for example start on a car object. A class method is a piece of code that is executed against the class for example makeNew on the Car class.

Objective C Classes and Objects

In this section we will show you how you would implement the above example with Objective C code. Lets first state some of the conventions in Objective C objects and classes.

Classes always begin with a capital letter, contain no spaces and every new word starts with a capital letter. Good examples of class names are: Car, Vehicle, Train, Bus, Tram, Desk, Bed, Sofa, SofaBed. Bad examples of class names are: car, vehicle, train, bus, TRAM, tram, desk, bed, sofa, sofaBed, SOFABed, sofabed. When creating objects in code the object names should start lowercase and then every new word start with a capital. For example: myNewCar.

Lets now define the Vehicle class using Objective C:

Lets digest what we just did there.

The first line is declaring an interface for the Vehicle class. The interface is almost like a contents file for the class, it is what the objects instantiated from the class are able to do. It contains all the properties, methods and instance variables for the class. The syntax for this line is @interface <CLASS NAME> : <SUPERCLASS NAME> {. Everything has a superclass, so at the top of the chain is NSObject, we are telling the compiler here that Vehicle only inherits from the base class NSObject. The code between the { on line 1 and the } on line 5 are our instance variables. This is where we will store the data that is put in our properties. They are private and cannot be accessed outside the class. A good coding practice is to name all your instance variables with a starting _. Lines 6-8 are our properties. Notice they are all objects too. One is a UIColor object, another an NSDate object and another an NSString object.

What are these stars? When you declare an object in Objective C, the syntax is: <CLASS NAME> *<OBJECT NAME>. So for example if we wanted to create a new car, and be able to access it with the name “ourCar” we would do Car *ourCar

Line 9 tells the compiler we are going to implement a method called startEngine that can be executed on every Vehicle object. This is an instance method. This is shown by the “-“.

Line 10 tells the compiler we are going to implement a method called createNew that can be executed on the Vehicle class. This is a class method. This is shown by the “+”.

The @end tells the compiler that we are finished defining the Vehicle class.

.h and .m

Most classes have two files, a .h and a .m. The .h file is the interface file (header file) and contains the interface for the class (the contents file if you like). The .m is the implementation file and implements all the methods, properties and any other logic required. When you want to use a class anywhere in your project you import the header file. The implementation file is what is compiled into your application.

What would the implementation look like for the vehicle class?

Line 1 imports the header so that we know what the class is to implement. Line 2 tells the compiler the implementation of Vehicle starts here. Lines 3-5 is a special objective c keyword that links a property to an instance variable. @synthesize colour = _color; is the equivalent to setting a getter and setter for the property accessing the instance variable. Lines 7-9 is our dummy implementation for startEngine, and lines 10-13 is our dummy implementation for createNew. Notice there is no semicolon at the end of the first line of each method, instead an open brace to show the implementation is inside. Also notice the createNew method is designed to return a new vehicle (the one that has been made) so the method needs to return a new vehicle object.

As an example of a subclass lets take a quick look at defining our Car class.
Car.h

Car.m

This should all look familiar now. One thing to notice, we set out superclass to Vehicle, this means that we can call startEngine and createNew and a Car object. Also all Car objects have properties color, dateManufactured and registrationNumber as well as the new model and doors properties.

Calling Methods on Objects and Classes

One other thing, when you are coding and want to access these methods and properties, objective c is pretty neat when doing this.

Look at this piece of code:

Line 1 creates a new car object called myNewCar. It calls alloc on the Car class – this is a built in class method from NSObject (our superclass’s superclass), that sets up a new empty object. We then call init on the allocated object. This sets up the Car object for use.

Line 2 sets the color property (from the Vehicle super class), to a UIColor object that represents red. The square bracket notation is how you call a method on a object or class:

Line 3 sets the number of doors to an NSNumber with value 5.

Line 4 calls the startEngine method (from our superclass) on our car object.

Line 5 calls the openBoot method (from our Car class) on our car object.

We hope this has helped explain a little bit how classes and objects work. If you are still confused, please comment below.