Introduction
qObjective-C is implemented as set of extensions to the C language.
qIt's designed to give C a full capability for object-oriented programming, and to do so in a simple and straightforward way.
qIts additions to C are few and are mostly based on Smalltalk, one of the first object-oriented programming languages.
qObjective C is simply a super set of c.
qSyntax of all non object oriented operations are similar as C.
qObjective-C is the primary language used for Apple's Cocoa API, and it was originally the main language on NeXT'sNeXTSTEP operating system.
qRest syntax is taken from smalltalk.
qEverything happens at run time(Every error is just a warning).
Data & Operation
qProgramming languages have traditionally divided the world into two parts—data and operations .
qObject-oriented programming groups operations and data into modular units called objects and lets you combine objects into structured .
qEvery object has both state (data) and behavior (operations on data).
q such as an ordinary bottle combine state (how full the bottle is, whether or not it’s open, howwarm its contents are) with behavior (the ability to dispense its contents at various flow rates).
ID
qid is a data type used by
Objective-C to define a pointer of an
object .
qAny type of object, as long as it is an object, we can use the id dat a type.
qFor example, we can define an
object by:
id anObject;
qnil is the reserved word for null
object
ex:
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
id dataValue;
Fraction *f1 = [[Fraction alloc] init];
Complex *c1 = [[Complex alloc] init];
[f1 setTo: 2 over: 5];
[c1 setReal: 10.0 andImaginary: 2.5];
// first dataValue gets a fraction
dataValue = f1;
[dataValue print];
// now dataValue gets a complex number
dataValue = c1;
[dataValue print];
[c1 release];
[f1 release];
[pool drain];
return 0;
}
Defining a Class
qIn Objective-C, classes are defined in two parts:
1.An interface that declares the methods and properties of the class and names its superclass
2.An implementation that actually defines the class (contains the code that implements its methods)
qThe declaration of a class interface begins with the compiler directive @interface and ends with the directive @end.
v @interface ClassName : ItsSuperclass
// Method and property declarations.
@end
Inheritance
qObjective-C enables programmer to inherit common methods and properties from other class, known as inheritance.
q Class from methods and properties are inherited known as Base Class and class that inherits known as Derived .
q Every inheritance hierarchy begins with a root class that has no superclass.
ex:
#import <Foundation/Foundation.h>
// ClassA declaration and definition
@interface ClassA: NSObject
{
int x;
}
-(void) initVar;
@end
@implementation ClassA
-(void) initVar
{
x = 100;
}
@end
// Class B declaration and definition
@interface ClassB : ClassA
-(void) printVar;
@end
@implementation ClassB
-(void) printVar
{
NSLog (@”x = %i”, x);}
@end
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
ClassB *b = [[ClassB alloc] init];
[b initVar]; // will use inherited method
[b printVar]; // reveal value of x;
[b release];
[pool drain];
return 0;
Polymorphism
qpolymorphism in the context of object-oriented programming, is the ability to create a variable, a function, or an object that has more than one form.
qThe purpose of polymorphism is to implement a style of programming called message-passing in the program.
qThe ability of objects to respond differently to the same message or function call.
ex:
// Shared Method Names: Polymorphism
#import “Fraction.h”
#import “Complex.h”
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Fraction *f1 = [[Fraction alloc] init];
Fraction *f2 = [[Fraction alloc] init];
Fraction *fracResult;
Complex *c1 = [[Complex alloc] init];
Complex *c2 = [[Complex alloc] init];
Complex *compResult;
[f1 setTo: 1 over: 10];
[f2 setTo: 2 over: 15];
[c1 setReal: 18.0 andImaginary: 2.5];
[c2 setReal: -5.0 andImaginary: 3.2];
// add and print 2 complex numbers
[c1 print]; NSLog (@” +”); [c2 print];
NSLog (@”---------”);
compResult = [c1 add: c2];
[compResult print];
NSLog (@”\n”);
[c1 release];
[c2 release];
[compResult release];
// add and print 2 fractions
[f1 print]; NSLog (@” +”); [f2 print];
NSLog (@”----”);
fracResult = [f1 add: f2];
[fracResult print];
[f1 release];
[f2 release];
[fracResult release];
[pool drain];
return 0;
}
both the Fraction and Complex classes contain add: and print methods.
So when executing the message expressions
Abstraction
qAbstraction is "To represent the essential feature without representing the back ground details.“
qAbstraction is the process of hiding the working style of an object, and showing the information of an object in understandable manner.
qAbstraction is the process of hiding the working style of an object, and showing the information of an object in understandable manner.
Method Overriding
qMethod overriding, in object oriented programming, is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes.
ex:
// Overriding Methods
#import <Foundation/Foundation.h>
// ClassA declaration and definition
@interface ClassA: NSObject
{
int x;
}
-(void) initVar;
@end
@implementation ClassA
-(void) initVar
{
x = 100;
}
@end
// ClassB declaration and definition
@interface ClassB: ClassA
-(void) initVar;
-(void) printVar;
NSLog (@”x = %i”, x);}
@end
@implementation ClassB
-(void) initVar // added method
{
x = 200;
}
-(void) printVar
{
NSLog (@”x = %i”, x);
}
@end
int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
ClassB *b = [[ClassB alloc] init];
[b initVar]; // uses overriding method in B
[b printVar]; // reveal value of x;
[b release];
[pool drain];
return 0;
}
Object-Oriented Relations
Composition:
q it gives us a ‘part-of’ relationship.
qMember Object can’t survive or exist outside the enclosing or containing class.
Aggregation:
qit gives a ‘has-a’ relationship.
q member Object can’t survives or exist without the enclosing class.
q it doesnot imply ownerShip,object can exist independently of each other.
Association:
qIt’s also a relation in which there is no OwnerShip or Container.
qAssociation is a relationship between two objects.association defines multiplicity between objects.
Conclusion
qAn object-oriented approach to application development makes programs more intuitive to design, faster to develop, more amenable to modification, and easier to understand.
qThe Objective-C language is a programming language designed to enable sophisticated object-oriented programming. Objective-C is defined as a small but powerful set of extensions to the standard ANSI C language. Its additions to C are mostly based on Smalltalk, one of the first object-oriented programming languages
........Thank you...........
No comments:
Post a Comment