Concepts of Programming Languages

(Sean Pound) #1
12.6 Support for Object-Oriented Programming in Objective-C 549

clearly a strong argument in favor of C++. Of course, all of the compiled lan-
guages that support object-oriented programming are approximately 10 times
faster than Smalltalk.

12.6 Support for Object-Oriented Programming in Objective-C


We discuss the support for object-oriented programming in Objective-C relative
to that of C++. These two languages were designed at approximately the same
time. Both add support for object-oriented programming to the C language. In
appearance, the largest difference is in the syntax of method calls, which in C++
are closely related to the function calls of C, whereas in Objective-C they are
more similar to the method calls of Smalltalk.

12.6.1 General Characteristics


Objective-C, like C#, has both primitive types and objects. Recall that a class
definition consists of two parts, interface and implementation. These two parts
are often placed in separate files, the interface file using the .h name extension
and the implementation using the .m name extension. When the interface is in
a separate file, the implementation file begins with the following:

#import "interface_file.h"

Instance variables are declared in a brace-delimited block following the
header of the interface section. Objective-C does not support class variables
directly. However, a static global variable that is defined in the implementation
file can be used as a class variable.
The implementation section of a class contains definitions of the methods
declared in the corresponding interface section.
Objective-C does not allow classes to be nested.

12.6.2 Inheritance
Objective-C supports only single inheritance. Every class must have a parent
class, except the predefined root class named NSObject. One reason to have a
single root class is that there are some operations that are universally needed.
Among these are the class methods alloc and init. The parent class of a new
class is declared in the interface directive after the colon that is attached to the
name of the class being defined, as in the following:

@interface myNewClass: NSObject {

Because base class data members can be declared to be private, subclasses
are not necessarily subtypes. Of course, all of the protected and public data
Free download pdf