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:
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:
// Definition of the interface for the program yourComputer.m
@interface NewComputer: NSObject
You would then import this file into the file you store your implementation code in:
// Definition of the implementation code for the program yourComputer.m
NSLog (@”The model number is %i”,modelNumber);
NSLog (@”The amount of ram is %i”,ram);
-(void) setModelNumber: (int) m
modelNumber = m;
-(void) setRam (int) m
ram = m;
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;
You could also pass and receive object’s using the generic object data type id.
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:
@interface Pet: NSObject
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:
@interface Dog: Pet
/* We don’t need to change all the variables, but we do need to change the makeNoise() method*/
@implementation Dog: Pet
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:
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.
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