Home > Articles > Home & Office Computing > Mac OS X

Cocoa Language Options

Scott Anguish, Erik Buck, and Donald Yacktman describe the general features of all languages that can be used with Cocoa, and provide a brief overview of object-oriented terminology.
This chapter is from the book

This chapter is from the book

In This Chapter

  • Object Orientation

  • Java

  • Objective-C

  • Other Languages

  • 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++.

Object Orientation

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

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

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.

Classes

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.

Instances

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.

NOTE

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.

Objects

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.

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

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

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

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.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020