Objective c

1,112 views

Published on

This presentation is about objective-c basics, it will give a brief overview of the language in a simple manner.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,112
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Objective c

  1. 1. Objective-C Object Oriented Programming By Shashank Chaturvedi IIIT-B
  2. 2. History <ul><li>It was created primarily by Brad Cox and Tom Love </li></ul><ul><li>in early 80’s at company name StepStone. </li></ul><ul><li>Both had been introduced to SmallTalk while at ITT Corporation Programming Technology Center in 1981. </li></ul><ul><li>Brad and Tom Recognized that SmallTalk would be invaluable in building Development environment for system developer at ITT’s. </li></ul><ul><li>Backward Compatibility with C was critically important in ITT’s </li></ul><ul><li>Telecom Engg. </li></ul>
  3. 3. History (Continued…) <ul><li>Love joined Schlumberger Research and in 1982 he acquire the first commercial copy of SmartTalk-80 in the company . </li></ul><ul><li>Love and Cox later formed new venture to commercialize their product which coupled Objective-C compiler with class libraries. </li></ul><ul><li>In between Steve Jobs left Apple Inc. and started the company NexT in 1988 and Licensed Objecive-C from StepStone. </li></ul><ul><li>NexT was later acquired by Apple Inc. in 1996, And used Objective-C in their new OS name MAC OS X </li></ul>
  4. 4. Why Objective-C? <ul><li>C is a procedural language, functions and data are separate. </li></ul><ul><li>C is also called static language. </li></ul><ul><li>All functions have to exist before you call. </li></ul><ul><li>All the variable have to be of specific type. </li></ul><ul><li>There is no Runtime Checking. </li></ul>
  5. 5. Basic – C Language <ul><li>C is a programming language developed at AT & T’s Bell Laboratories of USA in 1972. </li></ul><ul><li>It was designed and written by a man named Dennis Ritchie. </li></ul><ul><li>C had the seeds of Object Oriented concept with structure. </li></ul><ul><li>Each structure definition is a template for a certain kind of thing. </li></ul>
  6. 6. Basic – C Language (Continued…) <ul><li>Structure Definition </li></ul><ul><li># include < stdio.h > </li></ul><ul><li>Struct xyz </li></ul><ul><li>{ </li></ul><ul><li>char* title; </li></ul><ul><li>int length; </li></ul><ul><li>} x1 ; </li></ul><ul><li>Void main() </li></ul><ul><li>{ </li></ul><ul><li>x1 .length =10; </li></ul><ul><li>x1 .title = “ Hello World ”; </li></ul><ul><li>} </li></ul>
  7. 7. What is Objective-C? <ul><li>Objective-C is implemented as set of extensions to the C language. </li></ul><ul><li>Objective-C incorporates C, you get all the benefits of C when working within Objective-C. </li></ul><ul><li>It's designed to give C a full capability for object-oriented programming, and to do so in a simple and straightforward way. </li></ul><ul><li>Its additions to C are few and are mostly based on Smalltalk, one of the first object-oriented programming languages. </li></ul><ul><li>Objective-C is the most dynamic of the object-oriented languages based on C. Most decisions are made at run time </li></ul>
  8. 8. What is Object Oriented ? <ul><li>Object-oriented programs are built around objects . </li></ul><ul><li>An object associates data with the particular operations that can use or affect that data. </li></ul><ul><li>In Objective-C, these operations are known as the object’s methods; the data they affect are its instance variables </li></ul><ul><li>An object bundles a data structure (instance variables) and a group of procedures (methods) into a self-contained programming unit. </li></ul>
  9. 9. Object Oriented Concepts <ul><li>CLASS DEFINATION </li></ul><ul><li>Done in two files. </li></ul><ul><li>First file have Class Declaration also known as Interface, and saved with “ .h ” extension. </li></ul><ul><li>Second file have class methods implementation known as Implementation, saved with “ .m ” extension. </li></ul><ul><li>Three access mechanism is permitted “ Public, Private, Protected ” By default everything is protected. </li></ul>
  10. 10. Object Oriented Concepts (Continued…) <ul><li>Class Declaration ( xyz. h ) </li></ul><ul><li># import < Cocoa/Cocoa.h> </li></ul><ul><li>@ interface XYZ : NSobject </li></ul><ul><li>{ </li></ul><ul><li>id test; </li></ul><ul><li>int test1; </li></ul><ul><li>} </li></ul><ul><li>- ( void ) InstanceMethod; </li></ul><ul><li>+ ( void ) ClassMethod; </li></ul><ul><li>@ end </li></ul>
  11. 11. Object Oriented Concepts (Continued…) <ul><li>Class Implementation ( xyz .m ) </li></ul><ul><li># import “ xyz.h ” </li></ul><ul><li>@ Implementation XYZ </li></ul><ul><li>+ ( void ) ClassMethod </li></ul><ul><li>{ </li></ul><ul><li>printf ( “I am ClassMethod” ); </li></ul><ul><li>} </li></ul><ul><li>- ( void ) InstanceMethod </li></ul><ul><li>{ </li></ul><ul><li>printf ( “Hello World” ); </li></ul><ul><li>} </li></ul><ul><li>@ end </li></ul>
  12. 12. Object Oriented Concepts (Continued…) <ul><li> Program Execution Main.m </li></ul><ul><li># import “ xyz.h ” </li></ul><ul><li># import < stdio.h > </li></ul><ul><li>void main(int argc, const char*argv[]) </li></ul><ul><li>{ </li></ul><ul><li>xyz* Myobject = [xyz new]; </li></ul><ul><li>[Myobject InstanceMethod]; </li></ul><ul><li>[xyz ClassMethod]; </li></ul><ul><li>} </li></ul>
  13. 13. Creating Class Object <ul><li>ClassName* ObjectName = [ ClassName new]; </li></ul><ul><li>for e.g : - NSString* mystring = [ NSString new]; </li></ul><ul><li>NSString* mystring =@” Hello World ”; </li></ul><ul><li>ClassName* ObjectName = [ ClassName alloc ] ; </li></ul><ul><li>ClassName* ObjectName = [ [ ClassName alloc ] init ]; </li></ul><ul><li>Where “ init ” is default constructor inherited from root class . </li></ul><ul><li>&quot; new &quot; is not a keyword in Objective-C, but NSObject implements a class method </li></ul><ul><li>&quot; new &quot; which simply calls &quot;alloc&quot; and &quot;init&quot; </li></ul><ul><li>We can call methods on classes too, for creating objects. </li></ul><ul><li>NSString* mystring = [ NSString string]; </li></ul><ul><li>// this is more convenient automatic style, Autorelease object created. </li></ul>
  14. 14. Object lifetime / Memory management <ul><li>In any program, it is important to ensure that objects are deallocated when they are no longer needed, otherwise your application’s memory footprint becomes larger than necessary. </li></ul><ul><li>Objective-C offers two mechanisms for memory management that allow you to meet these goals. </li></ul><ul><li>Reference counting :- where you are ultimately responsible for determining the lifetime of objects, Each part of the application keeps track of only its own references for an object rather than total use throughout the entire application. </li></ul><ul><li>Objective-C and most other object oriented languages always has an initialization method. Initialization method is created to setup default values for things. For example :- </li></ul><ul><li>-init { if ( self = [ super init ] ) </li></ul><ul><li>title = “Untitled Photo” </li></ul><ul><li>return self; </li></ul><ul><li>} </li></ul>
  15. 15. Object lifetime / Memory management <ul><li>The counterpart to initialization method is a “cleanup” method </li></ul><ul><li>which is created to cleanup memory for any data or to display </li></ul><ul><li>message on console. For example :- </li></ul><ul><li>- dealloc { [ super dealloc ]; </li></ul><ul><li>[ Object release ] ; </li></ul><ul><li> printf ( “Deallocating Photon” ); </li></ul><ul><li>} </li></ul><ul><li>. </li></ul><ul><li>Every Object has retain count, which counts how many other objects want to keep that object. </li></ul><ul><li>Retain count must be maintained in the code. </li></ul><ul><li>[ Object retain ]; // increment count </li></ul><ul><li>[ Object release ]; // decrement count </li></ul><ul><li>Objective-C runtime takes responsibility for freeing the memory when all of the references are released i.e. retain count reaches to Zero. </li></ul>
  16. 16. Object lifetime / Memory management <ul><li>NSString* mystring = [ NSString alloc ] init ]; // memory allocation, increase the retain count by +1 </li></ul><ul><li>[ mystring release ]; // releasing the references, Decrease the retain count by -1 </li></ul><ul><li>We can use autorelease to save manual retain release of object. </li></ul><ul><li>ClassName* ObjectName = [ [ ClassName alloc ] autorelease ] ; </li></ul><ul><li>num of alloc + retains = num of release + autorelease </li></ul><ul><li>Garbage collection :- where you pass responsibility for determining the lifetime of objects to an automatic “collector.” </li></ul><ul><li>NSString* mystring = [ NSString string]; </li></ul>
  17. 17. Class constructor/Destructor <ul><li>Constructors in Objective-C are technically just &quot;init&quot; methods, they aren't a special construct like they are in C++ and Java. </li></ul><ul><li>The default constructor is -(id) init { self = [super init]; return self; } ; </li></ul><ul><li>Similar to Java, Objective-C only has one parent class. </li></ul><ul><li>Accessing it's super constructor is done through [super init] </li></ul><ul><li>and this is required for proper inheritance. </li></ul><ul><li>This returns an instance which you assign to another new keyword, self. Self is similar to this in Java and C++. </li></ul><ul><li>If ( self ) is the same as if ( self != nil ) to make sure that the super constructor successfully returned a new object. nil is Objective-C's form of NULL from C/C++. This is gotten from including NSObject. </li></ul>
  18. 18. Constructor/Destructor declaration <ul><li>User define constructor should be declared in “ .h ” file as </li></ul><ul><li>-( XYZ * ) initWithNumerator: (int) n denominator: (int) d ; </li></ul><ul><li>Constructor defination is in “ .m ” file as </li></ul><ul><li>-( XYZ * ) initWithNumerator: (int) n denominator: (int) d </li></ul><ul><li>{ </li></ul><ul><li>self = [super init]; </li></ul><ul><li>if ( self ) { </li></ul><ul><li>[ self setNumerator: n andDenominator: d]; </li></ul><ul><li>} </li></ul><ul><li>return self ; </li></ul><ul><li>} </li></ul><ul><li>Destructor method is called when object is deleted. It releases all the instance variables of the class. </li></ul><ul><li>- dealloc { [ super dealloc ]; </li></ul><ul><li>[ Object release ] ; </li></ul><ul><li> printf ( “Deallocating Photon” ); </li></ul><ul><li>} </li></ul>
  19. 19. Types of Methods <ul><li>Two types of Methods are there Class Methods and </li></ul><ul><li>Instance Method . </li></ul><ul><li>Class Method : Begin with “+” operator, it called on class itself. +(void) Init </li></ul><ul><li>for e.g. :- [ClassName Method] // Method call </li></ul><ul><li>[ XYZ init ] </li></ul><ul><li>Instance Method : Begin with “-” operator, it called on class object instance. –(void) Display </li></ul><ul><li>for e.g. :- ClassName* ObjectName = [ ClassName new]; </li></ul><ul><li>[ ObjectName Display ]; // Method call </li></ul>
  20. 20. Types of Methods (Continued…) <ul><li>Method can have multiple parameter / inputs </li></ul><ul><li>for e.g. :- </li></ul><ul><li>-(void) WritetoFile : (NSString*) Str Automatically : (BOOL) flag ); // calling this </li></ul><ul><li>method </li></ul><ul><li>[MyObject WritetoFile : “Hello World ” Automatically : true ]; </li></ul><ul><li>Method can return value </li></ul><ul><li>for e.g. :- Mydigit = [ MyObject Method ]; </li></ul>
  21. 21. Inheritance <ul><li>Root class is NSObject, Every class inherit from this class. </li></ul><ul><li>Like java, Objective-C also doesn’t permit multiple Inheritance. </li></ul><ul><li># import “ xyz.h “ </li></ul><ul><li>@ interface ABC : XYZ </li></ul><ul><li>{ </li></ul><ul><li> int test2; </li></ul><ul><li>// test1, test ( inherited version ) </li></ul><ul><li>} </li></ul><ul><li> - ( void ) Display; </li></ul><ul><li>// - ( void ) InstanceMethod ( inherited version) </li></ul><ul><li>@ end </li></ul>
  22. 22. Inheritance (Continued…) <ul><li>Instance Variables: The object of class ABC contains not only the instance variables that were defined for its class, but also the instance variables defined for its Superclass XYZ , all the way back to the root class NSObject. </li></ul><ul><li>Methods: An object has access not only to the methods that were defined for its class, but also to methods defined for its Superclass. </li></ul><ul><li>Method Overriding: Implement a new method with the same name as one defined in a class farther up the hierarchy. The new method overrides the original; instances of the new class will perform it rather than the original. </li></ul>
  23. 23. Polymorphism <ul><li>Each object has define its own method but for different class, they can have the same method name which has totally different meaning. </li></ul><ul><li>The two different object can respond differently to the same message. </li></ul><ul><li>Together with dynamic binding, it permits you to write code that might apply to any number of different kinds of objects, without your having to choose at the time you write the code what kinds of objects they might be. </li></ul>
  24. 24. Threading <ul><li>Objective-C supports multithreading in applications. </li></ul><ul><li>Objective-C provides support for thread synchronization. </li></ul><ul><li>The @synchronized()directive locks a section of code for use by a single thread. Other threads are blocked until the thread exits the protected code—that is, when execution continues past the last statement in the @synchronized() block. </li></ul><ul><li>The @synchronized() directive takes as its only argument any Objective-C object, including self. This object is known as a mutual exclusion semaphore or mutex . It allows a thread to lock a section of code to prevent its use by other threads </li></ul>
  25. 25. Exception Handling <ul><li>The Objective-C language has an exception-handling syntax similar to that of Java and C++. </li></ul><ul><li>An exception is a special condition that interrupts the normal flow of program execution. </li></ul><ul><li>Objective-C exception support involves four compiler directives: @try, @catch, @throw, and @finally : </li></ul><ul><li>1) Code that can potentially throw an exception is enclosed in a @try{} block. </li></ul><ul><li>2) A @catch{} block contains exception-handling logic for exceptions thrown in a </li></ul><ul><li>@try{} block. You can have multiple @catch{} blocks to catch different types of </li></ul><ul><li>exception. (For a code example, see “Catching Different Types of Exception.” ) </li></ul><ul><li>3) You use the @throw directive to throw an exception, which is essentially an </li></ul><ul><li>Objective-C object. You typically use an NSException object, but you are not </li></ul><ul><li>required to. </li></ul><ul><li>4) A @finally{} block contains code that must be executed whether an exception is </li></ul><ul><li>thrown or not. </li></ul>
  26. 26. Exception Handling (Continued…) <ul><li>Cup *cup = [[Cup alloc] init]; </li></ul><ul><li>@try { </li></ul><ul><li>[cup fill]; </li></ul><ul><li>} </li></ul><ul><li>@catch (NSException *exception) { </li></ul><ul><li>NSLog(@&quot;main: Caught %@: %@&quot;, [exception name], </li></ul><ul><li>[exception reason]); </li></ul><ul><li>} </li></ul><ul><li>@finally { </li></ul><ul><li>[cup release]; </li></ul><ul><li>} </li></ul><ul><li>Throwing Exceptions </li></ul><ul><li>NSException *exception = [ NSException exceptionWithName: @&quot;HotTeaException&quot; </li></ul><ul><li>reason: @&quot;The tea is too hot&quot;userInfo: nil]; </li></ul><ul><li>@throw exception; </li></ul>
  27. 27. Objective-C Vs C++ <ul><li>C++ supports multiple Inheritance, where as Objective-C doesn’t. </li></ul><ul><li>By default C++ supports Compile time binding, where as Objective-C supports runtime binding. </li></ul><ul><li>In C++ there is no Root Class concept. </li></ul><ul><li>In C++ we issue order while calling Methods, Where as in </li></ul><ul><li>Object . Method() ; </li></ul><ul><li>Objective-C we pass requesting message to Methods. </li></ul><ul><li>[ Object Method ] </li></ul>
  28. 28. Example <ul><li>access.h </li></ul><ul><li>#import <Foundation/NSObject.h> </li></ul><ul><li>@interface Access: NSObject </li></ul><ul><li>{ </li></ul><ul><li>@public int publicVar; </li></ul><ul><li>@private int privateVar; </li></ul><ul><li> int privateVar2; </li></ul><ul><li>@protected int protectedVar; </li></ul><ul><li>} </li></ul><ul><li>@end </li></ul>
  29. 29. Example (Continued…) <ul><li>access.m </li></ul><ul><li>#import &quot;Access.h&quot; </li></ul><ul><li>@implementation Access </li></ul><ul><li>{ </li></ul><ul><li>} </li></ul><ul><li>@end </li></ul>
  30. 30. Example (Continued…) <ul><li>main.m </li></ul><ul><li>#import &quot;Access.h&quot; </li></ul><ul><li>#import <stdio.h> </li></ul><ul><li>int main( int argc, const char *argv[] ) </li></ul><ul><li>{ </li></ul><ul><li>Access *a = [ Access new ]; </li></ul><ul><li>// works </li></ul><ul><li>a->publicVar = 5; </li></ul><ul><li>printf( &quot;public var: %in&quot;, a->publicVar ); </li></ul><ul><li>// doesn't compile </li></ul><ul><li>// a->privateVar = 10; </li></ul><ul><li>// printf( &quot;private var: %in&quot;, a->privateVar ); </li></ul><ul><li>[a release]; </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul>
  31. 31. References <ul><li>www.wikipedia.org </li></ul><ul><li>www.apple.com </li></ul><ul><li>http://www.otierney.net/objective-c.html </li></ul>
  32. 32. THANK YOU !!!

×