Tuesday, 14 August 2012

Inheritance & Runtime polymorphism & Categories and Protocols

Inheritance:-
From a terminology point of view,we can speak of classes, child classes, and parent
classes.Analogously,we can talk about classes, subclasses, and superclasses.You should become
familiar with both types of terminology.
Whenever a new class (other than a new root class) is defined, the class inherits certain
properties. For example, all the instance variables and the methods from the parent implicitly
become part of the new class definition.That means the subclass can access these
methods and instance variables directly, as if they were defined directly within the class
definition.

Ex.
.h
@interface ClassA: NSObject
{
int x;
}
-(void) initVar;
@end
.m
@implementation ClassA
-(void) initVar
{
x = 100;
}
@end
add a other class
.h
@interface ClassB : ClassA
-(void) printVar
@end
.m
@implementation ClassB
-(void) printVar
{
NSLog (@
x = %d, x);
}
@end

Main Program
{

ClassB *b = [[ClassB alloc] init];
[b initVar]; 
[b printVar];
return 0;
}


1- Single inheritance

.h
@interface student :NSObject{
int rno;
NSString *name;
}
-(id)initWithrno:(int)rno andbname:(NSString*)name;
-(void)display1;
@end
.m
#import "student.h"
@Implementation
-(id)initWithrno:(int)rno andbname:(NSString*)name
{
super=[super init];
if(super)
{
self->rno=rno;
self->name=name;
}
return self;
}
-(void)display1;
{
NSLog(@"Roll no is= %d",rno);
NSLog(@"Student name is=%@",name);
}
@end

Add a other class
.h
#import "student.h"
@Interface marks :student{

float phy,math,chem;
}
-(id)initWithphy:(float)phy andmath:(float)math andchem:(float)chem;
-(void)display;
@end
.m
@implementation
-(id)initWithphy:(float)phy andmath:(float)math andchem:(float)chem{
super=[super init];
if(self)
{
self->phy=phy;
self->math=math;
self->chem=chem;
}
return self;
}
-(void)display{
NSLog(@"phy=%f",phy);
NSLog(@"math=%f",math);
NSLog(@"chem=%f",chem);

} 
@end

Main Calss

#import "student.h"
#import"marks.h"

student *obj=[[student alloc]initWithrno:101 andbname:@"abc"];
marks *obj1=[[marks alloc]initWithphy:50 andmath:75 andchem:35];
[obj1 display];
[obj1 display1];
 @end

Method Overriding:-

.h
@interface student :NSObject{
int rno;
NSString *name;
}
-(id)initWithrno:(int)rno andbname:(NSString*)name;
-(void)display;
@end
.m
#import "student.h"
@Implementation
-(id)initWithrno:(int)rno andbname:(NSString*)name
{
super=[super init];
if(super)
{
self->rno=rno;
self->name=name;
}
return self;
}
-(void)display1;
{
NSLog(@"Roll no is= %d",rno);
NSLog(@"Student name is=%@",name);
}
@end

Add a other class
.h
#import "student.h"
@Interface marks :student{

float phy,math,chem;
}
-(id)initWithname:(NSString*)name andrno:(int)rno andphy:(float)phy andmath:(float)math andchem:(float)chem;
-(void)display;
@end
.m
@implementation
-(id)initWithname:(NSString*)name andrno:(int)rno andphy:(float)phy andmath:(float)math andchem:(float)chem{


super=[super initWithrno:100 andbname:@"abc"];
if(self)
{
self->phy=phy;
self->math=math;
self->chem=chem;
}
return self;
}
-(void)display{
[super display];   // call a parent class method
NSLog(@"phy=%f",phy);
NSLog(@"math=%f",math);
NSLog(@"chem=%f",chem);
 }
@end

Main Program

# import "student.h"
#import"marks.h"

marks *obj=[[marks alloc]initWithname:@"abc" andrno:101 andphy:102 andmath:106 andchem:108];       
[obj dispaly];
@end

Runtime Polymorphism :-


.h
@interface student :NSObject{
int rno;
NSString *name;
}
-(id)initWithrno:(int)rno andbname:(NSString*)name;
-(void)display;
@end
.m
#import "student.h"
@Implementation
-(id)initWithrno:(int)rno andbname:(NSString*)name
{
super=[super init];
if(super)
{
self->rno=rno;
self->name=name;
}
return self;
}
-(void)display1;
{
NSLog(@"Roll no is= %d",rno);
NSLog(@"Student name is=%@",name);
}
@end

Add a other class
.h
#import "student.h"
@Interface marks :student{

float phy,math,chem;
}
-(id)initWithname:(NSString*)name andrno:(int)rno andphy:(float)phy andmath:(float)math andchem:(float)chem;
-(void)display;
@end
.m
@implementation
-(id)initWithname:(NSString*)name andrno:(int)rno andphy:(float)phy andmath:(float)math andchem:(float)chem{


super=[super initWithrno:100 andbname:@"abc"];
if(self)
{
self->phy=phy;
self->math=math;
self->chem=chem;
}
return self;
}
-(void)display{
[super display];   // call a parent class method
NSLog(@"phy=%f",phy);
NSLog(@"math=%f",math);
NSLog(@"chem=%f",chem);
 }
@end

Main Program

# import "student.h"
#import"marks.h"

marks *obj=[[marks alloc]initWithname:@"abc" andrno:101 andphy:102 andmath:106 andchem:108];   
student *stu=[[student alloc]init]
stu=obj;    
[stu display];    //here called a chaild class method
@end


Categories  :- Some time you might  be working witha class defination want to add some new methods to it thats called a cataegories.
.h
@Interface student: NSObject{
NSString *name;
int rno;
float phy, chem,math total;
}
-(void)setrno:(int)r name:(NSString*)nm ;
-(void)show;
 @end
.m
@implementation
-(void)setrno:(int)r name:(NSString*)nm
{
rno=r;
name=nm;
}
-(void)show{
NSLog(@"roll no is=%d",rno);
NSLog(@"roll no is=%@",name);
}
@end

add a other calss:-
class name student-test
.h
#import"student.h"
@interface student(Test)
-(void)setphy:(float)ph  chem :(float)chem math :(float)math;
-(void)calculate;
-(void)display;
@end
.m 
@implementation
-(void)setphy:(float)ph  chem :(float)ch math :(float)ma{
phy=ph;
chem=ch;
math=ma;
}
-(void)calculate{

total=(phy+chem+math);
}
-(void)display{
NSLog(@"phy=%f",phy);
NSLog(@"chem=%f",chem);
NSLog(@"math=%f",math);
NSLog(@"total=%f",total);
}
@end

Main Program
#import "student.h"
#import"student-test.h"

student *obj =[student new];
[obj setrno:10 name:@"abc"];
[obj show];
[obj setphy:50  chem :60 math :55];
[obj calculate];
[obj display];
@end

Protocol :-A prottocol is a list of methods that is shatred a among classes .
if you have decides to implement all of the methods for a particular protocol your are said to confirm or adopt that protocol.

Note :- Protocol content only method decarlation and does not content a method defination

Syntax:-

@interface calss name:NSObject<protocol1, protocol2........>

.h
add there a protocol class and see that fomate
#import <foundation/foundation.h>
@Interface Display:NSObject {

}
-(void)display;
@end
and a other class demo
.h
#import"Display.h"
@interface demo:NSObject<Display>
{
}
.m
-(void)display
{
NSLog(@"Hello");

}
@end
Main program
#import"Display.h"
#import"Demo.h"

Demo *d=[demo new];
[d display];
@end

No comments:

Post a Comment