Developing for iPhone

Learn how to create and develop iPhone applications from scratch.

Tagged with ‘classes’

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 »

I know you are probably itching to get stuck into the iPhone programming environment, but there are a few things you HAVE to learn first. One of them is Object Oriented Programming. The reason I am jumping right into this is that it is the main feature of Objective-C and a very powerful set of tools which you will need to understand if you want to create iPhone apps.

Let’s start by creating a new project in XCode. Fire up XCode and follow these steps:

  1. In XCode, click File > New Project
  2. Choose Command Line Utility > Foundation Tool
  3. Name the project “Objects Intro” and save it.

In the project window, click the Source folder in the left-hand pane to view your source files. You should have one called “Objects Intro.m” and one called “Objects Intro_Prefix.pch” (which you can ignore for now.)

Object oriented programming languages are based around the use of objects. Objects can represent real life objects (like a chair or a car), or can be more abstract. Objects can hold variables, change variables and pass information between each other. This makes the development of complex applications much more simple. You will understand them more as we continue. Let’s make an object right now!
 

Read the rest of this entry »