Introduction to Object Oriented Programming, 3rd Ed

Timothy A. Budd

Chapter 1

Thinking Object-Oriented

Outline

  1. Roadmap
  2. Conflicing Objectives
  3. Why has OOP Remained Popular for so long?
  4. A new Paradigm
    1. Sapir-Whorf Hypothesis
    2. Example from Computer Languages
      1. A Better Solution
      2. Why did the Second Programmer find a Better Solution
    3. Church's Conjecture
  5. Imperative Programming
    1. Visualization of Imperative Programming
  6. Why Not Build a Program out of Computers?
    1. Recursive Design
  7. Kays Description of Object-Oriented Programming
    1. Illustration of OOP Concepts -- Sending Flowers to a Friend
    2. Elements of OOP - Agents and Communities
    3. Elements of OOP - Objects
    4. Elements of OOP - Messages
    5. Information Hiding
    6. Elements of OOP - Receivers
    7. Different Receivers, Same Message, Different Actions
    8. Behavior and Interpretation
    9. Elements of OOP - Recursive Design
    10. Non-interference
    11. Elements of OOP - Classes
    12. Hierarchies of Categories
    13. A Class Hierarchy
    14. Elements of OOP - Inheritance
    15. Elements of OOP - Overriding
  8. Computing as Simulation
    1. Metaphor and Problem Solving
    2. Quote from newsweek
  9. Chapter Summary

Other Material

Intro OOP, Chapter 1, Outline

Roadmap

In this chapter we begin our exploration of object-oriented programming. Among the topics we will explore:

What is a paradigm? Why is this term used to describe the object-oriented approach to problem solving?

How does language influence thought?

What are some of the characteristics of the object-oriented way of thinking?

Intro OOP, Chapter 1, Slide 01

Conflicting Objectives

Along the way, I'll try to convince you the validity of the following two assertions: Both are true.
Intro OOP, Chapter 1, Slide 02

Why has OOP Remained Popular for so long?

OOP has been the dominant programming paradigm for more than twenty years. Why is it so popular? Nevertheless, programming is still a task that requires skill and learning.
Intro OOP, Chapter 1, Slide 03

A New Paradigm

Object-oriented programming is often described as a new paradigm.

We start by considering the definition of this term:

Par a digm n. A list of all the inflectional forms of a word taken as illustrative example of the conjugation or declension to which it belongs. An example or model. [Late Latein paradigma, from Greek paradeigma, modern paradeiknunai, to compare, exhibit.]

What is the world does this have to do with computer programming languages?

Intro OOP, Chapter 1, Slide 04

Sapir-Whorf Hypothesis

In linguistics there is a hypothesis that the language in which an idea or thought is expressed colors or directs in a very emphatic manner that nature of the thought:

What is true of natural languages is even more true of artificial computer languages

Intro OOP, Chapter 1, Slide 05

Example from Computer Languages

A student working in DNA research had the task of finding repeated sequences of M values in a long sequence of values:

    ACTCGGATCTTGCATTTCGGCAATTGGACCCTGACTTGGCCA ...

Wrote the simplest (and therefore, most efficient?) program:

    DO 10 I = 1, N-M
    DO 10 J = I+1, N-M
    FOUND = .TRUE.
    DO 20 K = 1, M
20  IF X[I+K-1] .NE. X[J+K-1] THEN FOUND = .FALSE.
    IF FOUND THEN ...
10  CONTINUE

Took a depressingly long time.

Intro OOP, Chapter 1, Slide 06

A Better Solution

A friend writing in APL found a much better solution by rearranging the data and sorting.

	A  C  T  C  G  G  positions 1 to M
	C  T  C  G  G  A  positions 2 to M+1
	T  C  G  G  A  T  positions 3 to M+2
	C  G  G  A  T  T  positions 4 to M+3
	G  G  A  T  T  C  positions 5 to M+4
	G  A  T  T  C  T  positions 6 to M+5
	 .  .  . 
	T  G  G  A  C  C 
	G  G  A  C  C  C
	 .  .  . 

Ran surprizingly quickly, thesis saved.

Intro OOP, Chapter 1, Slide 07

What lead to the discovery?

Why did the APL programmer find the better solution?

The fundamental point is that the language you speak leads you in one direction or another.

But what about the Sapir-Whorf hypothesis, that says there are some thoughts you can express in one language that you cannot express in another?

Intro OOP, Chapter 1, Slide 08

Church's Conjucture

In computation we have the following assertion:

Church's Conjecture: Any computation for which there exists an effective procedure can be realized by a Turing machine language.

Anything can be done in any language, but it may simply be easier or more efficient to use one language or another.

Would YOU want to write an event-driven GUI interface in Turing machine?

Bottom line: Languages lead you, but do not prevent you from going anywhere you want.

Intro OOP, Chapter 1, Slide 09

Imperative Programming

So, what are the paradigms of programming?

Imperative programming is the ``traditional'' model of computation.

A processing unit is separate from memory, and ``acts'' upon memory.

Intro OOP, Chapter 1, Slide 10

Visualization of Imperative Programming

Sometimes called the ``pigeon-hole'' model of computation.

Intro OOP, Chapter 1, Slide 11

Why Not Build a Program out of Computers?

Alan Kay thought about this conventional design of the computer, and asked why we constructed the whole out of pieces that were useless by themselves.

Why not build a whole out of pieces that were similar at all levels of detail? (Think of fractals).

Idea: A program can be build out of little computing agents.

Intro OOP, Chapter 1, Slide 12

Recursive Design

The structure of the part mirrors the structure of the larger unit.

Intro OOP, Chapter 1, Slide 13

Kay's Description of Object-Oriented Programming

Object-oriented programming is based on the priciple of recursive design.

  1. Everything is an object

  2. Objects perform computation by making requests of each other through the passing of messages

  3. Every object has it's own memory, which consists of other objects.

  4. Every object is an instance of a class. A class groups similar objects.

  5. The class is the repository for behavior associated with an object

  6. Classes are organized into singly-rooted tree structure, called an inheritance hierarchy.

We can illustrate these principles by considering how I go about solving a problem in real life.

Intro OOP, Chapter 1, Slide 14

Illustration of OOP Concepts -- Sending Flowers to a Friend

To illustrate the concepts of OOP in an easily understood framework, consider the problem of sending flowers to a friend who lives in a different city. Chris is sending flowers to Robin.

Chris can't deliver them directly. So Chris uses the services of the local Florist.

Chris tells the Florist (named Fred) the address for Robin, how much to spend, and the type of flowers to send.

Fred contacts a florist in Robins city, who arranges the flowers, then contacts a driver, who delivers the flowers.

If we start to think about it, there may even be other people involved in this transaction. There is the flower grower, perhaps somebody in charge of arrangments, and so on.

Intro OOP, Chapter 1, Slide 15

Agents and Communities

Our first observation is that results are achieved through the interaction of agents, which we will call objects.

Furthermore, any nontrivial activity requires the interaction of an entire community of objects working together.

Each object has a part to play, a service they provide to the other members of the community.

Intro OOP, Chapter 1, Slide 16

Elements of OOP - Objects

So we have Kay's first principle.

  1. Everything is an object.

Actions in OOP are performed by agents, called instances or objects.

There are many agents working together in my scenario. We have Chris, Robin, the florist, the florist in Robins city, the driver, the flower arranger, and the grower. Each agent has a part to play, and the result is produced when all work together in the solution of a problem.

Intro OOP, Chapter 1, Slide 17

Elements of OOP - Messages

And principle number 2:

  1. Objects perform computation by making requests of each other through the passing of messages

Actions in OOP are produced in response to requests for actions, called messges. An instance may accept a message, and in return will perform an action and return a value.

To begin the process of sending the flowers, Chris gives a message to Fred. Fred in turn gives a message to the florist in Robins city, who gives another message to the driver, and so on.

Intro OOP, Chapter 1, Slide 18

Information Hiding

Notice that I, as a user of a service being provided by an object, need only know the name of the messages that the object will accept.

I need not have any idea how the actions performed in response to my request will be carried out.

Having accepted a message, an object is responsible for carrying it out.

Intro OOP, Chapter 1, Slide 19

Elements of OOP - Receivers

Messages differ from traditional function calls in two very important respects:

Intro OOP, Chapter 1, Slide 20

Different Receivers, Same Message, Different Actions


var
	Fred : Florist;
	Elizabeth : Friend;
	Ken : Dentist;

begin
	Fred.sendFlowersTo(myFriend);  { will work }
	Elizabeth.sendFlowersTo(myFriend); { will also work }
	Ken.sendFlowersTo(myFriend); { will probably not work }
end;

The same message will result in different actions, depending upon who it is given to.
Intro OOP, Chapter 1, Slide 21

Behavior and Interpretation

Although different objects may accept the same message, the actions (behavior) the object will perform will likely be different.

The determination of what behavior to perform may be made at run-time, a form of late binding.

The fact that the same name can mean two entirely different operations is one form of polymorphism, a topic we will discuss at length in subsequent chapters.

Intro OOP, Chapter 1, Slide 22

Elements of OOP - Recursive Design

  1. Every object has it's own memory, which consists of other objects.

Each object is like a miniature computer itself - a specialized processor performing a specific task.

Intro OOP, Chapter 1, Slide 23

Non-interference

It is important that objects be allowed to perform their task however they see fit, without unnecessary interactions or interference with other objects.

Intro OOP, Chapter 1, Slide 24

Elements of OOP - Classes

  1. Every object is an instance of a class. A class groups similar objects.

  2. The class is the repository for behavior associated with an object.

The behavior I expect from Fred is determined from a general idea I have of the behavior of Florists.

We say Fred is an instance of the class Florist.

Behavior is associated with classes, not with individual instances. All objects that are instances of a class use the same method in response to similar messages.

Intro OOP, Chapter 1, Slide 25

Hierarchies of Categories

But there is more that I know about Fred then just that he is a Florist. I know he is a ShopKeeper, and a Human, and a Mammal, and a Material Objects, and so on.

At each level of abstraction I have certain information recorded. That information is applicable to all lower (more specialized) levels.

Intro OOP, Chapter 1, Slide 26

Class Hierarchies

picture of class hierarchy

Intro OOP, Chapter 1, Slide 27

Elements of OOP - Inheritance

  1. Classes are organized into a singly-rooted tree structure, called an inheritance hierarchy

Information (data and/or behavior) I associate with one level of abstraction in a class hierarchy is automatically applicable to lower levels of the hierarchy.

Intro OOP, Chapter 1, Slide 28

Elements of OOP - Overriding

Subclasses can alter or override information inherited from parent classes:

Inheritance combined with overriding are where most of the power of OO originates.

Intro OOP, Chapter 1, Slide 29

Computing as Simulation

Intro OOP, Chapter 1, Slide 30

Metaphor and Problem Solving

Because the OOP view is similar to the way in which people go about solving problems in real life (finding another agent to do the real work!), intuition, ideas, and understanding from everyday experience can be brought to bear on computing.

On the other hand, common sense was seldom useful when computers were viewed in the process-state model, since few people solve their everyday problems using pigeon-holes.

Intro OOP, Chapter 1, Slide 31

From Newsweek

``Unlike the usual programming method -- writing software one line at a time-- NeXT's ``object-oriented'' system offers larger building blocks that developers can quickly assemble the way a kid builds faces on Mr. Potato Head.''
Mr. Potato Head Picture

Intro OOP, Chapter 1, Slide 32

Summary

Intro OOP, Chapter 1, Slide 33