Object Oriented Programming

What is it?

How does it work?

Abstract Data Types

Computer programs manipulate data.

What they do to that data is important.

How they do it or what form the data takes is not.

So...create new data types and declare exactly what the program can do to manipulate a variable of that type.

This provides a form of data integrity.

And provides flexibility for future modification of the data's form (i.e. for efficiency, bug-fixing, or enhancement).

Terminology

In object-oriented languages, these data types are called classes.

A variable of a given class is an instance of that class.

The word object is often used to refer to an instance of a class (e.g., a Button object is an instance of the Button class).

The operations that a given class implements are called methods (not functions).

Messaging

We can ask an object to perform a method without knowing what its class is.

Instead of calling a function to perform some operation on an object, we send that object a message asking it to perform that operation on itself.

Depending on the class of the object, different code will be executed by the computer.

This is useful since a given message may be meaningful to different classes of objects (e.g. "size" is meaningful both to a stack and a queue even though each may calculate its size in a very different way).

Terminology:

The messages sent to an object are called messages.

Instead of calling a function, we send a message to an object to get it to perform one of its methods.

Example of Object Oriented Design

The Unix File System is object oriented. Most of you are familiar with the following Unix system calls:

  open()
  close()
  read()
  write()
  lseek()
  ioctl()

The open() call takes a file name as one of its arguments and returns a handle to a file. The open call may perform some device specific initialization, such as supplying DTR to a terminal line or locating disk drive which contains the named file. In the kernel, there is a switch statement which decides which device specific routines will be called based on the filename you have provided. Each of the other routines listed above have similar code to decide how to do buffering and actually locate the data that you are working with, either by sending the appropriate commands to the disk drive or tape drive, or waiting for serial input from a terminal line, or sending data to a printer.

As a person using the routines, you generally don't have to know which set of operations will be performed by the kernel, but that the kernel will do whatever is necessary to fulfill your request.

In this example, the class might be FileManipulation, the methods would be open(), close(), etc., and an object would be the file descriptor that the open() routine returns.

Inheritance

Related classes can share common code.

Terminology

A class which inherits from another class is a subclass of the class it inherits from. A class is a subclass of its superclass.

When a subclass responds to a message in a different way that its superclass does, the subclass is said to have overridden its superclass's method.

Subclass is often used as a verb: "I subclassed the Vehicle class to create the Car class."

Programming

The thought processes of the programmer are as much a part of object-oriented programming as any of the above.

Programs must be thought of as a collection of cooperating pieces of data (objects) rather than a thread of control.

Program design is data-oriented rather than process-oriented.

User-interface programming is fundamentally object-oriented.

Terminology

Object-oriented programmers use phrases like:
"When the user presses this button, it sends a message to this object which calculates something and then sends a message to this other object which updates this and..."
instead of:
"We wait for the user to press a button and then decide which one it was, and based on that, we decide what to do, then we wait for the user to do something else..."

Go on to an introduction to Objective-C, an extension of C which was designed for object oriented programming.