Cocoa Language Options
In This Chapter
Choosing a Language for Use with Cocoa
The Use of Objective-C in This Book
Cocoa is a collection of object-oriented components written in the Objective-C language. Objective-C is a flexible and dynamic language that adds object-oriented extensions to ANSI standard C. Because of the flexibility of Objective-C, the Cocoa components can be used by a wide variety of languages besides Objective-C. The key language elements needed to use Cocoa are support for dynamic object orientation and compatibility with the C programming language.
This chapter describes the general features of all languages that can be used with Cocoa, and a brief overview of object-oriented terminology. Details about the most common languages used with Cocoa are provided. The available language options are explained along with some of the advantages and disadvantages of different languages. This book primarily uses the Objective-C language, and this chapter explains why.
Java and many scripting languages provide the required language features to use Cocoa directly. Other languages such as C and C++ are not sufficiently dynamic to use Cocoa objects directly. C and C++ programs can access Cocoa in two ways: They can use Cocoa indirectly via the C-based Objective-C runtime, or they can be recompiled using the Objective-C and Objective-C++ language compilers.
Details about using the Objective-C runtime from C or C++ without using the Objective-C language syntax are provided in Appendix A, "Unleashing the Objective-C Runtime." Because Objective-C is an extension of standard C and can compile all C programs, the best way to use Cocoa from C code is to actually use Objective-C. Parts of a program can be standard C (perhaps for portability reasons), whereas other parts can use Objective-C's object-oriented extensions to access Cocoa. A variant of Objective-C called Objective-C++ provides the same object-oriented extensions to C++ that Objective-C provides to standard C. The best way to mix C++ code and Cocoa is to use Objective-C++.
Cocoa is a collection of objects. To understand how various languages use Cocoa, a brief explanation of objects and object orientation is needed. Object-oriented languages must use objects in a way that is compatible with Cocoa to be integrated with Cocoa. For example, Java is an object-oriented language that interfaces well with Cocoa. C++ is an object-oriented language that provides a model of objects incompatible with Cocoa.
The goals of object orientation are to make writing software easier, cheaper, and faster. The principal way that object orientation achieves its goals is through software reuse. The idea is that software can be organized into objects that are individually reusable. Each time a new software project begins, substantial parts of the project can be implemented using existing software objects. In theory, the only new code that is written for a project is the code that is truly unique to that project and cannot be shared by other projects.
By reusing objects, programmers hope to reduce the amount of new code written for each project and, therefore, finish the project faster. Reused objects are already well-tested in other projects. By reusing objects, programmers avoid bugs that might be created in new code. Existing objects that implement complex logic are used to make creating software for a new project easier. The idea is that the most complex and difficult to write code is provided by existing objects. Reusing objects is simpler than rewriting the logic implemented by the objects.
Encapsulation is one of the principal ideas of object orientation. Encapsulation means that data and the logic that operates on the data are grouped together. Data is only modified via the operations encapsulated with the data. Encapsulation aids reuse and simplifies software maintenance. Encapsulation also ensures that related data and logic can be easily identified in one project and reused in another. Because data and logic are encapsulated together, it is not necessary to grab a line of code here and a data structure definition there, or search all the source code in a project for necessary lines of code to reuse just one feature of a project. Encapsulation aids software maintenance by localizing problem solutions. If a bug is detected, or a new feature must be added, encapsulation ensures that there is only one place in the code where changes are made. Without encapsulation, fixing a bug, or adding a feature might require changes to many different parts of a software project.
Modularity is related to encapsulation. Modularity is the practice of implementing a software project in multiple modules. A module is a self-contained input to a compiler. The idea is that modules of code and data can be developed and compiled independently. The separately compiled modules are brought together to complete a project. In many cases, each module encapsulates some data and logic. Apple's Objective-C compiler enables the use of multiple modules to encapsulate one set of data and operations. It is also possible to implement multiple units of encapsulation in one module, but it is usually a bad practice.
In most object-oriented languages, something called a class is the basis of encapsulation. Each class encapsulates some data and all operations upon that data. Operations on data are sometimes called behaviors. Classes are implemented in one or more modules. Classes formalize the ideas of encapsulation, and in some languages the compiler enforces that encapsulation. The compiler prevents code that is not part of a class from modifying the data managed by the class. Classes are related to instances and inheritance.
A class is in some respects an abstract idea. A class describes data and operations on that data, but a class by itself is usually just an idea. For example, imagine a class called Control that describes certain characteristics of all graphical user interface elements. Those characteristics might include color and position. The class Control is not any particular user interface element, it describes all user interface elements. A particular button is an "instance" of the class Control. An instance of the class Control has a color and a position as described by the Control class.
A class describes data and behavior. An instance actually stores the data described by a class. There can be any number of instances of a class. The behaviors defined in a class are applied to instance's data.
You will learn more about abstract classes and abstract Cocoa classes in Chapters 4 and 7. Chapter 4 also introduces the details of Objective-C as well as class methods versus instance methods. If these concepts are confusing to you now, you'll get more details in these two chapters.
Both classes and instances can be called objects. Classes are objects that describe instances. Instances are objects that store data described by a class. Object is a general term that applies to encapsulated data and logic and has different implications in different languages. Almost every object-oriented language endows objects with capabilities beyond just encapsulation, such as support for specialization.
Object orientation promotes software reuse. Software objects from one project can be used in another project. But what happens when a new project needs an object similar to one that already exists, but needs just a few changes? The existing object can be specialized rather than starting from scratch to create a new object that meets the exact needs of the new project. Specialization is a technique for altering parts of the data and behavior of an existing object while reusing other parts. There are two types of specialization: instance-based and class-based. Cocoa uses both class-based specialization and instance-based specialization extensively.
Instance-based specialization is a technique for changing the behavior of just one instance object without necessarily modifying the behavior of other instances of the same class.
Class-based specialization applies changes to classes. The most common technique is to create a new class that includes all the data and operations of another class while adding additional data and new behaviors. Instances of the new class store the additional data and have the additional behaviors along with the data and behaviors of instances of the original class.
Inheritance is the most common form of class-based specialization. If a class called Button includes the data and behaviors of class Control, class Button is said to inherit from class Control. The terms subclass and superclass describe the inheritance relationship. Button is a subclass of Control. A subclass inherits all of the data and behavior of its superclass. In this example, Control is Button's superclass. If class Button inherits from class Control, an instance of Button can be used in any context that an instance of Control is required.
Some languages such as C++ support multiple inheritance. Multiple inheritance means that a class has all the data and behavior described by two or more super classes. Java and Objective-C do not support multiple inheritance, and Cocoa does not use multiple inheritance.
Messages are one way that objects can communicate with each other. Messages enable objects to request that other objects invoke behaviors. A user interface object might send the isEnabled message to a Control instance as a way to request that the Control instance returns a YES or NO value. A message is an abstract idea and few assumptions are made about messages. For example, a message can be sent to an anonymous object. The sender of the message might not know the class of the receiver. The receiver might not even be in the same program as the sender. Messages promote object reuse by minimizing the dependencies between objects. The less one object knows about another, the better chance the objects can be reused separately. Messaging enables communication between objects without dependencies.
Many object-oriented languages do not directly support messaging. Messaging is one of the dynamic features of Objective-C that are essential to the implementation of Cocoa. Messaging is described in Chapter 4, and the technical implementation of messaging is described in Appendix A.
Polymorphism is another technique that enables the reuse of software objects. Polymorphism allows the software that uses an object to work regardless of the specific class of the object. In other words, polymorphism enables an object to send the same message to different receivers without knowing precisely what behavior will be invoked by the message when it is received.
All messages in Objective-C use polymorphism. In many cases it is not possible for the sender of an Objective-C message to know the class of the object that receives the message. Each receiver will invoke different behaviors upon receipt of the message. Java and C++ also support polymorphism to various degrees. Along the spectrum of flexibility, C++ compilers require detailed knowledge about all objects whose behaviors are used. Objective-C does not require any knowledge about the objects that are used at compile time. Java falls between the two extremes.