codescracker


objective c

Objective-C Inheritance



« Previous Tutorial Next Tutorial »


Inheritance allows you to define a class in terms of another class which makes it easier to create and maintain an application. This also provides you, an opportunity to reuse the code functionality and fast implementation time.

Whenever creating a class, then instead of writing the completely new data members and member functions, you can designate that the new class should inherit the members of an existing class. And this existing class is called as the base class, and the new class is referred to as the derived class in Objective-C.

Base Class and Derived Class in Objective-C

Objective-C language only allows, multiple inheritance, that is, it can have only one base class but allows multilevel inheritance. All the Objective-C classes is derived from the superclass NSObject. Now let's take a look at this code fragment:

@interface derived-class: base-class

Objective-C Inheritance Example

Here is an example program, illustrates the concept of inheritance in Objective-C. Here there is a base class Shape and its derived class Rectangle.

/* Objective-C Inheritance - Example Program */
		
#import <Foundation/Foundation.h>
 
@interface Person : NSObject
{
	NSString *person_name;
	NSInteger person_age;
}
- (id)initWithName:(NSString *)name andAge:(NSInteger)age;
- (void)print;
@end

@implementation Person
- (id)initWithName:(NSString *)name andAge:(NSInteger)age
{
	person_name = name;
	person_age = age;
	return self;
}
- (void)print
{
	NSLog(@"Name = %@", person_name);
	NSLog(@"Age = %ld", person_age);
}
@end

@interface Employee : Person
{
	NSString *employee_edu;
}
- (id)initWithName:(NSString *)name andAge:(NSInteger)age 
  andEducation:(NSString *)education;
- (void)print;
@end

@implementation Employee
- (id)initWithName:(NSString *)name andAge:(NSInteger)age 
  andEducation: (NSString *)education
{
	person_name = name;
	person_age = age;
	employee_edu = education;
	return self;
}
- (void)print
{
	NSLog(@"Name = %@", person_name);
	NSLog(@"Age = %ld", person_age);
	NSLog(@"Education = %@", employee_edu);
}
@end

int main(int argc, const char * argv[])
{
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];        
	NSLog(@"Base class Person Object");
	Person *person = [[Person alloc]initWithName:@"Harshit" andAge:5];
	[person print];
	NSLog(@"Inherited Class Employee Object");
	Employee *employee = [[Employee alloc]initWithName:@"Harshit" 
	andAge:5 andEducation:@"MBA"];
	[employee print];        
	[pool drain];
	
	return 0;
}

When the above code is compile and executed, it will produce the following result:

2010-10-03 13:18:37.932 Inheritance[349:303] Base class Person Object
2010-10-03 13:18:37.932 Inheritance[349:303] Name = Harshit
2010-10-03 13:18:37.932 Inheritance[349:303] Age = 5
2010-10-03 13:18:37.932 Inheritance[349:303] Inherited Class Employee Object
2010-10-03 13:18:37.932 Inheritance[349:303] Name = Harshit
2010-10-03 13:18:37.932 Inheritance[349:303] Age = 5
2010-10-03 13:18:37.933 Inheritance[349:303] Education = MBA

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test