Tutorial 2-4: Inheritance
Another reason object-oriented programming languages are so powerful is inheritance. Inheritance lets a class inherit methods and variables from another class. To demonstrate this, let’s make a new class that will inherit the Television class we already created. Open your “Objects Intro” project and click File > New File. Select Cocoa from the left pane and Objective-c Class from the right pane. Click next and name it “MutableTelevision.m” (make sure “create .h” box is ticked) then click Finish.
Ok, now we have two classes in our project. “Television” and “MutableTelevision”. The Television class does the basic things that we would expect a television to do. But what if we want a class that does all that but has extra features? We could write all the methods again for the new class, but an easier option would be to use inheritance and make MutableTelevision a child of Television.
Open “MutableTelevision.h” and let’s start creating the interface file. Edit it to look like this:
#import <Cocoa/Cocoa.h> #import "Television.h" @interface MutableTelevision : Television -(void) mute; @end
Let’s look at what we did. First, as always, we tell the compiler that we are creating an interface file, then we name it. But then we tell it that the Television class is the parent of this class by using the : operator. Notice that you need to import the parent class. Look at “Telelvision.h” again – you will notice that we seem to have inherited a class called NSObject. We didn’t create that class, it is already set up for us by Apple. You don’t need to understand everything this class does, but we have used one of it’s methods before. The NSLog method for printing text to the console is simply a method from the NSObject class.
Another name for parent and child classes is superclasses and subclasses. They mean the same thing – but it is useful to know both terms. So, we know that MutableTelevision is a child (or subclass) of Television, and Television is a child (or subclass) of NSObject. Even though MutableTelevision does not directly inherit NSObject, it inherits everything in the Television class including it’s parent class (or superclass), NSObject. We did not define any instance variables because the channel variable is already inherited from Television.
Once you understand this, save “MutableTelevision.h” and open “MutableTelevision.m” to fill out that mute method. Edit the file to look like this:
#import "MutableTelevision.h" @implementation MutableTelevision -(void) mute { NSLog(@"The TV is now on mute."); } @end
A very simple method here that just prints a message to the console. Save and close “MutableTelevision.h”, then open “Objects Intro.m”. Edit it to look like this:
#import <Foundation/Foundation.h> #import "MutableTelevision.h" int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; int chanChange; MutableTelevision *myTV = [[MutableTelevision alloc] init]; NSLog(@"Type in the channel number:"); scanf("%i", &chanChange); [myTV changeChannel: chanChange]; [myTV mute]; [myTV release]; [pool drain]; return 0; }
The main changes here are the import statements importing “MutableTelevision.h” instead of “Television.h” and the creation of the MutableTelevision object. Notice that we are still using the name myTV as the object name, except now the object is created by the MutableTelevision class rather than the Television class. Run the program to test it out with the new mute method. You may notice that the changeChannel method worked even though we didn’t define it in “MutableTelevision”. This is because MutableTelevision has inherited all the methods and variables from the Television class. If we tried to use the mute method with a Television object however, it wouldn’t work because there is no mute method in the Television class or in any class above it in the hierarchy.
We still have a lot to learn about Objective-C, but you are probably hoping to start working with the iPhone interface. So in our next tutorials I will introduce development in the iPhone environment while introducing more Objective-C concepts as we go. Play around with what you have learned so far.
When you are ready, move on to the next tutorial.







1 Response
[...] Tutorial 2-2: Properties and Accessor Methods Tutorial 2-4: Inheritance Apr [...]
Posted on April 23rd, 2009 at 12:56 pm
Add A Comment