iPhone Development: Objective C Pt 5

Object Oriented ProgrammingObject Oriented Programming with Objective C

In a previous article, I explained how you create object’s, instance variable’s and method’s in Objective C. In this article, I’ll explain Inheritance, Polymorphism and go over other OOP topic’s I missed previously.

Briefly, Inheritance provide’s you with a way to build additional capability’s onto existing class’s.

Polymorphism is a little more complicated. It allows you to call a parent object method and it will sort out which class object method should be executed.

For example, let’s say you defined a parent class named pet. You derived a new child class called dog, from the class pet. Polymorphism allows you to call the method pet.makeNoise() and it automatically know’s to use the dog.makeNoise() method.

If that didn’t make sense, don’t worry about it. Many example’s will nail the idea home.

OOP Concept’s I Missed

Here I’ll cover all the OOP capability’s I missed in the previous article. Then I’ll move on to inheritance and polymorphism. They include:

  • How to separate the interface & implementation file’s from your program
  • How to automatically generate get & set function’s for your variable’s
  • How to handle multiple different argument’s sent to your method’s
  • How to pass objects to a method

Separate the Interface & Implementation File’s from the Program

It is considered good practice to keep your class definition’s separate from your actual program. You want to store the @interface definition in a file with the same name as the code that contains your @implementation code, but with the extension .h. So, if your @implementation code is stored in NewComputer.m, the file that contain’s the interface would be named NewComputer.h.

The interface code would look like this:

// NewComputer.h

// Definition of the interface for the program yourComputer.m

#import <Foundation/Foundation.h>

@interface NewComputer: NSObject

{

int modelNumber;

int ram;

}

-(void) getModelNumber;

-(void) getRam;

-(void) setModelNumber;

-(void) setRam;

@end

You would then import this file into the file you store your implementation code in:

// NewComputer.m

// Definition of the implementation code for the program yourComputer.m

#import “NewComputer.h”

@implementation NewComputer

-(void) getModelNumber

{

NSLog (@”The model number is %i”,modelNumber);

}

-(void) getRam

{

NSLog (@”The amount of ram is %i”,ram);

}

-(void) setModelNumber: (int) m

{

modelNumber = m;

}

-(void) setRam (int) m

{

ram = m;

}

@end

In your actual program that contain’s the main function, you would now import the class definition’s with this line of code: #import “NewComputer.h”

The compiler knows where to find both the implementation and interface code at compile time. The actual program looks the same except for the added #import statement.

Automatically Generate Get & Set Function’s

In the class above, the only function’s I defined where the get and set instance variable function’s. This is so commonly done that Objective C provides a way for you to automatically generate these function’s. I could have generated them with this line of code:

@synthesize modelNumber, ram;

Now if I want to be returned the value of modelNumber I would just call self.modelNumber, like this:

NSLog(@”The model number is %i”, self.modelNumber);

If I was working with an object derived from NewComputer named yourComputer I would get the model number with: yourComputer.modelNumber. I could set the model number by referring to yourComputer.setmodelNumber.

Handle Multiple Different Argument’s Sent to your Method’s

Maybe you want to be able to handle different argument’s as a feature, or for error handling. What ever the reason, you can define multiple method’s with the same name, but different arguments easily. Here is an example:

-(int) multiplyThis: (int) intX: (int) intY;

-(double) multiplyThis: (double) doubleX: (double) doubleY;

This is known as method overloading. Notice that I changed the name’s of the argument’s here. This is seen as a short coming in Objective C, because most language’s allow you to perform method overloading without the need for changing the argument name.

Just remember, you can define more than one method with the same name, but they must contain argument’s with different name’s.

Pass Object’s to a Method

You can pass object’s to method’s in Objective C quite easily. This function will accept our yourComputer object and then return it:

-(NewComputer *) doSomeStuff: (NewComputer *) comp

{

// changeComp will store the change’s made to comp

NewComputer *changeComp = [[NewComputer alloc] init];

changeComp.setModelNumber = 324;

changeComp.setRam = 10;

return changeComp;

}

You could also pass and receive object’s using the generic object data type id.

Inheritance

When explaining Inheritance I like to use a specific example. Let us say we define a super class named Pet. Here is it’s definition:

#import <Foundation/Foundation.h>

@interface Pet: NSObject

{

NSString *name;

NSString *home;

NSString *noise;

NSString *favoriteFood;

}

-(void) makeNoise;

@end

@implementation Pet

-(void) makeNoise

{

NSLog (@”%@”,self.noise);

}

@end

Every pet makes a noise of some kind, but they all are different. For example a dog barks. With inheritance we can make a dog class of type pet, but overwrite just the method that needs to change, like this:

#import “Pet.h”

@interface Dog: Pet

/* We don’t need to change all the variables, but we do need to change the makeNoise() method*/

-(void) makeNoise;

@end

@implementation Dog: Pet

-(void) makeNoise

{

NSLog (@”Bark”);

}

@end

Now if the make noise method is called for Pet type Dog, you will see “Bark” as a response. Inheritance just means what it says:

  • Use or inherit what you need
  • Replace what is different
  • Add new method’s that are needed like chaseMailMan()

Polymorphism

As we used inheritance principle’s previously to overwrite the makeNoise() method, we now use Polymorphism to call the right method depending on which object we are using. Polymorphism allows us to have two different class definition’s with the same method name.

  • If we reference an object of type Dog, through it’s superclass Pet those method’s that weren’t overwritten will execute.
  • Also, those method’s that were overwritten, by the class Dog, will be executed instead of the previously defined Pet method’s

It’s like the example I gave in the previous article. If you know how to operate a car, you can drive any other specific type of car, even if you don’t know it’s name. The method car.driveForward() can be executed even if the derived hummer.driveForward() overwrote the car class.

That’s All Folk’s

I hit you with a ton of information on Object Oriented Programming with Objective C. In the next couple of article’s I will show you example program’s until you can design just about anything on the iPhone, or iPad.

Leave question’s below and I’ll answer them.

Here to Serve

Think Tank

4 Responses to “iPhone Development: Objective C Pt 5”

  1. Perry says:

    In the tutorial above, you wrote:

    “In your actual program that contain’s the main function, you would now import the class definition’s with this line of code: #import “Fraction.h””

    Was this a typo and supposed to be “#import “NewComputer.h”?

    I just want to make sure I understood this correctly.

  2. ed says:

    Hi Derek,

    I stumbled on your objective c video tutorials and then it just stopped on part 4… where can I find the rest of your video tutorials please. They were well documented, presented and meticulously thought out for general presentation – congratulations…

    I cannot join twitter etc.. as I work for defence hence wondering if you can provide me links sources to view the rest of your objective Tutorials.

    Thanking you in anticipation
    ed

    • admin says:

      I’m glad you found the Objective C tutorial useful. I had to take down the rest because I recreated a current app and got a cease and desist order. I’m going to get back into Objective C as soon as my Python tutorial is done in the next week. If there is anything specific you’d like to see done just ask 🙂

Leave a Reply

Your email address will not be published.

Google+