Developing for iPhone

Learn how to create and develop iPhone applications from scratch.

Tagged with ‘inheritance’

Tutorial 2-4: Inheritance

Posted by Henry On April - 23 - 2009 1 COMMENT

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
 
</cocoa>

Read the rest of this entry »