1 Objective C 2.0 Programming Language Final Exam C SC 520 (Principles of Programming Languages) Report prepared by: Bhavin Mankad Karthik Ravichandra CS 520: Final Project Report Objective-C2 Table of Content Page # 1. History 3 2. Design Objectives 3 a. Object Orientation b. Simple Extension to C, Strict Superset of C c. Light Weight d. Flexibility to switch between Structural and Object Oriented Programming e. Dynamic Binding f. Reflection 3. Interface and Implementation 4 4. Class and Object 5 5. Inheritance 6 6. Dynamic Behavior 7 7. Messaging 8 8. Exception Handling 9 9. Categories 9 10.Properties 10 11.Memory Management 10 12.Interesting Facts about Objective-C 12 13.Comparison with C++ 12 14.References 12 CS 520: Final Project Report Objective-C3 1. History With languages like Simula and SmallTalk, the importance of Object Oriented Programming was becoming apparent in early 1980s. Object Oriented Programming made development and maintenance of large scale projects a lot easier as compared to Structured Programming. Also, thinking in Object Oriented terms was closer to real world situations. Brad Cox, working in a company called StepStone was much interested in Software Reuse and Object Oriented development and was greatly influenced by simplicity and Object Oriented features of SmallTalk. In early 1980s, while working at StepStone, he incorporated SmallTalk like Object Oriented features to C compiler and developed a new language called Objective-C. In 1986, Cox put much of the description of his new language in a book named ‘Object Oriented Programming, An evolutionary approach’. The company started by Steve Jobs, NeXT, licensed Objective-C from StepStone and released their own version of compiler and APIs in 1988. NeXTStep, their Object Oriented multi-tasking operating system was based on Objective-C. OpenStep was developed by NeXT in partnership with Sun Microsystems. Work on GNU’s version of NeXTstep clone started in 1992 and it was given the name ‘GNUstep’. The first compiler written by Dennis Glatting in 1992 was followed by another version written by Richard Stallman. After NeXT was acquired by Apple, the OpenStep was used in their operating system Mac OS-X. Apple’s one of the most important frameworks Cocoa is developed in OpenStep interface objects. Thus Apple is the single most influential force behind existence of Objective-C in the market place today. 2. Design Objectives a. Object Oriented Programming The primary design goal of Objective-C was Object Oriented Programming. The Object Oriented syntax is much influenced by SmallTalk. b. Simple extension to C The language is a strict super-set of C. Thus it is backward compatible with C. It was designed to add Object Oriented support to C. Only those Object Oriented concepts which were absolutely essential were added into the language. This has made the language much simpler to learn and program in. CS 520: Final Project Report Objective-C c. Light Weight The language does not need any run-time platform like Virtual Machine for Java. Language Pre-processor translates this syntax into C source code. The language just adds a SmallTalk style messaging and some syntactical sugar for defining and working with objects in C language’s environment. This keeps the language light weight at run time. The language does not have additional syntactical and conceptual quirks like virtual functions, friend functions, templates, multiple inheritance etc in C++.4 d. Flexibility As Objective-C is a strict superset of C, it can support pure structural programming as well. This gives flexibility to programmers who may want to use structural programming like C for systems level code and OO programming for application development. e. Dynamic Behavior Objective C is the most dynamic of all OO languages. It defers most of the decisions till run time. It supports Dynamic Typing, Dynamic Binding and Dynamic Loading. These concepts are further explained in section 6. f. Reflection Objective C supports reflection. It can observe and modify its structure at runtime using the ‘Class’ object methods and also paradigms like ‘selector’. The concept is similar to Reflection APIs in Java. 3. Interface and Implementation In Objective C, the code is organized in two types of source files. Interface files declare a class with its instance variables and methods. Implementation files contain the implementation of these methods. The objective behind this separation is to achieve ‘Data Abstraction’ – an important concept in Object Oriented Programming. Interface describes contract for the given class stating which methods (messages) this class is responsible for responding to and what data it holds. This hides the implementation details and exposes only the contract to other classes which use this class. The users of this class don’t have to worry about implementation. Thus any updates in a class method’s implementation will not affect the user classes. Interface • This declares a class named Rectangle which inherits from the class Shape • length and width are instance variables. Instance variables are declared within { } • Methods are declared outside the { } • In Method declaration, (+) means it’s a class method and (–) means it is an instance method. First ( ) declares return type of the method. For example (void) or (float). Then comes the method name followed by ‘:’ incase the method has arguments. Arguments follow the ‘:’ with types given in ( ) Rectangle.h CS 520: Final Project Report Objective-C @interface Rectangle : Shape { float length; float width; } + (void)alloc; -(float) setLength: (int)len andWidth: (float) width; @end5 Implementation • Implementation file contains definition of methods for the Rectangle class • Similar to the interface, (+) shows it is a class method and (–) shows that it is an instance method Rectangle.m 4. Class and Object Class These are the building blocks of any Object Oriented language. Objective C defines classes in the interface header files as explained in the previous section. For example in the previous section, Rectangle.h file defines a class named Rectangle. Each class can have instance and class variables and
View Full Document