When you created the Person class, you declared it to be a subclass of NSObject. This means that every instance of Person will have the instance variables and methods defined in NSObject as well as the instance variables and methods defined in Person. We say that Person inherits the instance variables and methods from NSObject. In this section, we are going to delve a bit into inheritance.
Open up the BMITime project and create a new file: an Objective-C class. Name it Employee and create it as a subclass of NSObject. Soon, we will change the Employee class to be a subclass of Person. Makes sense, right? Employees are people. They have heights and weights. However, not all people are employees. We’ll also add an employee-specific instance variable to our class – an employee ID.
Open Employee.h. Import Person.h, change the superclass to Person, and add an instance variable to hold the employee’s ID number:
#import "Person.h" @interface Employee : Person { int employeeID; } @property int employeeID; @end
Open Employee.m and synthesize the accessors:
#import "Employee.h" @implementation Employee @synthesize employeeID; @end
Now you have a new Employee class with all the instance variables of Person and a new instance variable called employeeID. Instances of Employee will respond to all the same messages that an instance of Person will. Instances of Employee will also respond to the messages setEmployeeID: and employeeID.
And, because Person inherits from NSObject, Employee also inherits all the instance variables and methods from NSObject. All objects inherit (either directly or indirectly) from NSObject.
NSObject has many methods, but only one instance variable: the isa pointer. Every object’s isa pointer points back to the class that created it.
Let’s say that you send the message fido to an object. In order to respond to this message, the object uses the isa pointer to find its class and ask, “Do you have an instance method named fido?” If the class has a method named fido, it gets executed. If the class doesn’t have a fido method, it asks its superclass, “Do you have an instance method called fido?” And up, up the chain it goes on the hunt for a method named fido. The hunt stops when the method is found or when the top of the chain is reached. At the top of the chain, NSObject says, “Nope, no fido method.” Then you get an error message that says something like “This instance of Employee does not respond to the fido selector.”
Try it. Open up main.m and send the instance of Person a message it won’t understand:
int main(int argc, const char * argv[]) { @autoreleasepool { // Create an instance of Person id person = [[Person alloc] init]; // Give the instance variables interesting values [person setWeightInKilos:96]; [person setHeightInMeters:1.8]; // Call the bodyMassIndex method float bmi = [person bodyMassIndex]; NSLog(@"person (%d, %f) has a BMI of %f", [person weightInKilos], [person heightInMeters], bmi); [person count]; } return 0; }
Build and run the program. (Ignore the warning from the compiler because you’re doing this on purpose. In most cases, you’ll want to pay attention to this warning if you see it!) You should see a runtime exception logged to the console:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Person count]: unrecognized selector sent to instance 0x100108de0'
After examining the exception, delete the problematic line before continuing.
You’ve created this new Employee class, but you haven’t used it yet. Change main.m to use Employee:
#import <Foundation/Foundation.h> #import "Employee.h" int main(int argc, const char * argv[]) { @autoreleasepool { // Create an instance of Person Person * person = [[Employee alloc] init]; // Give the instance variables interesting values [person setWeightInKilos:96]; [person setHeightInMeters:1.8]; // Call the bodyMassIndex method float bmi = [person bodyMassIndex]; NSLog(@"person (%d, %f) has a BMI of %f", [person weightInKilos], [person heightInMeters], bmi); } return 0; }
Notice that your person variable is still declared as a pointer to a Person. Think this will cause a problem? Build and run the program, and you’ll see that your program still works fine. This is because an employee is a kind of person – it can do anything a person can. That is, we can use an instance of Employee anywhere that the program expects an instance of Person.
Now, however, you’re going to use a method that is unique to Employee, so you must change the type of the pointer variable:
#import <Foundation/Foundation.h> #import "Employee.h" int main(int argc, const char * argv[]) { @autoreleasepool { // Create an instance of Person Employee *person = [[Employee alloc] init]; // Give the instance variables interesting values [person setWeightInKilos:96]; [person setHeightInMeters:1.8]; [person setEmployeeID:15]; // Call the bodyMassIndex method float bmi = [person bodyMassIndex]; NSLog(@"Employee %d has a BMI of %f", [person employeeID], bmi); } return 0; }
To review, when a message is sent, the search for the method of that name starts at the object’s class and goes up the inheritance hierarchy. The first implementation that is found is the one that gets executed. Thus, you can override inherited methods with a custom implementation. Let’s say, for example, that you decided that employees always have a Body Mass Index of 19. In this case, you might override the bodyMassIndex method in Employee. Open Employee.m and do so:
#import "Employee.h" @implementation Employee @synthesize employeeID; - (float)bodyMassIndex { return 19.0; } @end
Build and run the program and note that your new implementation of bodyMassIndex is the one that gets executed – not the implementation from Person.
Notice that you implemented bodyMassIndex in Employee.m, but you didn’t declare it in Employee.h. Declaring a method in the header file advertises the method so that instances of other classes can call it. However, because Employee inherits from Person, everyone already knows that instances of Employee have a bodyMassIndex method. There is no need to advertise it again.