Sunday, 7 June 2015

Polymorphism in objective c

Polymorphism In Objective C


Polymorphism is the name suggest poly means many and morphism means forms.
So the aggregate be many+forms or manyforms.

In general polymorphism is the ability to variables,method and object to take on many  forms. In practical form you can say that there are situations arises in case of inheritance when the inherit class has method which has same name as of base class but having different behaviour then base class method. So the condition occurring in front of compiler to choose which method as execution. Polymorphism can also occurring at run time then there Virtual function help it in C programming language.But in objective C virtual function does not support.
But in Objective C all functions or metods are virtual method calls are determined at run time.
Also Different reaction of method or message in objective c when different input data is given.
Let us look up towards the different types of polymorphism.
Be kept in mind there are two types of polymorphism


1.)    Compile Time Polymorphism.(Overloading-Operator and method).
2.)    Run Time Polymorphism.(Virtual)[Not supported in objective c].

Why we use Polymorphism?

Let us quickly look why polymorphism is need for programmer. There are some situations occurs in front of developer when he wants to use same name of method for both base class and derived class but with different execution of code. So the overload of method occurs. In such case compiler try to solve this problem which method to execute.

StudentID and StudentName can be set according to need in below example.
Example of polymorphism:

In Teacher.h

@interface Teacher:NSObject
{
Integer physics,chemistry,math;
CGFloat Percentage;
}

-(void)showMarks;
@end;
In Teacher.m

@implementation Teacher
-(void)showMarks
{
NSLog(@”The %@ name %@ got  %f  percents”,StudentID,StudentName,Percentage);
}


In ScienceStudent.h;

@interfaece ScienceStudent:Teacher
@property(nonatomic,strong)NSString *studentname;
@property(nonatomic,strong)NSInteger *studentID;
@property(nonatomic,strong)NSInteger *physicsmarks;
@property(nonatomic,strong)NSInteger *mathmarks;
@property(nonatomic,strong)NSInteger *chemistrymarks;
-(id)initWithMarks:(NSInteger *)physicsmarks chemistry:(NSInteger *)chemistrymarks mathmarks:(NSInteger *)mathmarks; 
-(void) calculateMarks;
@end;

In ScienceStudent.m;

@implementation ScienceStudent

@synthesize studentID=_studentID;
@synthesize studentName=_studentName;
@synthesize physicsMarks=_physicsMarks;
@synthesize mathMarks=_mathMarks;
@synthesize chemistryMarks=_chemistryMarks;
-(id)initWithScienceMarks:(NSInteger *)physicsmarks chemistry:(NSInteger *)chemistrymarks mathmarks:(NSInteger *)mathmarks; 

{

cemistry=self. cemistrymarks;
pysics=self.physicsmarks;
math=self.mathmarks;

return self;
}
-(void)calculateMarks
{
Percentage=physics*chemistry*math/300;
}
@end;


In ComputerStudent.h

@interface ComputerStudent:ScienceStudent
{
Integer computer;
}
@property(nonatomic,strong)NSInteger *computermarks;
-(id)initWithComputerMarks:(NSInteger *)physicsmarks chemistry:(NSInteger *)chemistrymarks mathmarks:(NSInteger *)mathmarks computermarks:(NSInteger *)computermarks;

In ComputerStudent.m

@implementation ComputerStudent
@synthesize computerMarks=_computerMarks;
-(id)initWithComputerMarks:(NSInteger *)physicsmarks chemistry:(NSInteger *)chemistrymarks mathmarks:(NSInteger *)mathmarks computermarks:(NSInteger *)computermarks
{
cemistry=self. cemistrymarks;
pysics=self.physicsmarks;
math=self.mathmarks;
computer=self.computermarks;
return self;
}
-(void)calculateMarks
{
Percentage=physics*chemistry*math*computer/400;
}

What is self keyword;
You will be many times familiar with self keyword. Self keyword is the respond with same object from which method or message is calling.

Now what to do in main function

Int main(int argc, const char * argv[])
{
NSAutorealesePool *pool=[[NSAutorealesePool alloc]init];
Teacher *student1=[[ScienceStudent alloc] initWithScienceMarks:78chemistry:65 mathmarks:93];
[student1 calculateMarks];
[student1 showMarks];   

Teacher *student2=[[ComputerStudent alloc] initWithComputerMarks:78chemistry:65 mathmarks:93 computermarks:89];

[student2 calculateMarks];
[student2 showMarks];  
[pool drain];
Return 0;
}
In the above example based on the availability of methos Showmarks and calculate either the method is executed in base or derived class executed.
.
I hope you like my this tutorial.

My next tutorial will be Data Encapsulation.






No comments:

Post a Comment