Objective C Tutorial - Objective C Inheritance






Class Inheritance

@interface MyClass : NSObject {

@private
    int position;
    NSString *name;
    
}

@property (assign) int position;
@property (retain) NSString *name;

-(int)changePosition: (int)myPosition;

@end

@implementation MyClass

@synthesize position;
@synthesize name;

- (id)init {
    if ((self = [super init])) {
        // Initialization code here.
    
    }
    
    return self;
}

- (void)dealloc {
    // Clean-up code here.
    [name release];
    [super dealloc];
}

-(int)changePosition: (int)myPosition
{
    int newPosition;
    newPosition = self.position + myPosition;
    return newPosition;
}

@end


@interface NewClass : MyClass {
@private
    
}

@end

@implementation NewClass

@end

#import <Foundation/Foundation.h>
 
int main ()
{
  MyClass *testObject = [[MyClass alloc] init];
    testObject.position = 45;
    NSLog (@"The object's current position = %i", testObject.position);
    testObject.position = [testObject changePosition:10];
    NSLog (@"The new object position = %i", testObject.position);
    
    NewClass *newObject = [[NewClass alloc] init];
    newObject.position = 32;
    NSLog (@"The object's current position = %i", newObject.position);
    newObject.position = [newObject changePosition:10];
    NSLog (@"The new object position = %i", newObject.position);  
 
   return 0;
}

The code above generates the following result.





Method Overriding

@interface MyClass : NSObject {

@private
    int position;
    NSString *name;
    
}

@property (assign) int position;
@property (retain) NSString *name;

-(int)changePosition: (int)myPosition;

@end

@implementation MyClass

@synthesize position;
@synthesize name;

- (id)init {
    if ((self = [super init])) {
        // Initialization code here.
    
    }
    
    return self;
}

- (void)dealloc {
    // Clean-up code here.
    [name release];
    [super dealloc];
}

-(int)changePosition: (int)myPosition
{
    int newPosition;
    newPosition = self.position + myPosition;
    return newPosition;
}

@end

@interface NewClass : MyClass {
@private
    
}

-(int)changePosition: (int)myPosition;

@end



@implementation NewClass

- (id)init {
    if ((self = [super init])) {
        // Initialization code here.
    }
    
    return self;
}

- (void)dealloc {
    // Clean-up code here.
    
    [super dealloc];
}

-(int)changePosition: (int)myPosition
{
    int newPosition;
    newPosition = self.position * myPosition;
    return newPosition;
}

@end


#import <Foundation/Foundation.h>
 
int main ()
{
  MyClass *testObject = [[MyClass alloc] init];
    testObject.position = 45;
    NSLog (@"The object's current position = %i", testObject.position);
    testObject.position = [testObject changePosition:10];
    NSLog (@"The new object position = %i", testObject.position);
    
    NewClass *newObject = [[NewClass alloc] init];
    newObject.position = 32;
    NSLog (@"The object's current position = %i", newObject.position);
    newObject.position = [newObject changePosition:10];
    NSLog (@"The new object position = %i", newObject.position);  
 
   return 0;
}

The code above generates the following result.