Intro to CS: Object-Oriented Programming
Dr. Scott Smith

(these notes are http://www.cs.jhu.edu/~scott/misc/intro2cs.html)
The Assignment is here.

Me

My Courses

600.321/421 (fall)
Smalltalk, object-oriented programming, object-oriented design, team programming
600.426 (odd years spring)
Programming language design & implementation
600.428 (even years spring)
Compiler Writing

My Research

Typed object-oriented programming languages

Objects and Programming

Goal of this lecture: what is object-oriented programming and why is it a good thing?

Some governing principles behind good programming language design

Machine language is not very human friendly.
Machine language today is a lot less friendly than it used to be because nobody progams in machine language anymore.

Most advanced languages satisfy 1. of human-friendly by dividing programs into procedures (functions) and modules (groups of functions and other definitions). C & Pascal aren't so good at the module part.

Object-Oriented Programming Languages (OOP): a school of language design.

Aimed at improved intuitions for programs beyond C/Pascal.

OO programming language examples

C++

+'s -'s

Smalltalk

+'s -'s

Java

between C++ and Smalltalk

Why Objects in Programming?

Thesis: Humans understand the world by viewing it as composed of autonomous, interacting objects.
Examples: humans, paper invoice, animals, nut and bolt, phone, spark plug, car

Real-World object properties:

Active, autonomous behavior
not always directly controlled by us on the outside (smoke detector beeps upon smoke)
Communicative
can "send messages" object to object (talk/command/push button)
Collaborative
long-term collaborative relationships between objects will arise (disk drive/CPU/memory/.. of a fixed computer; ant colony)
Encapsulated
some of the properties/behaviors are not externally visible (though some internals uncovered by sending messages: "are you hungry?")
Nested
complex objects have other objects as components (which in turn may have object components etc) (computer example)
Uniquely named
(though not always -- tension between wanting short name vs unambiguous name)
Self-aware
(possibly; note this does not require self-consciousness, dogs are aware of their own itches on their bodies)
Creation/Destruction
May be created or destroyed
Virtual Objects: objects that live in programs only.
  • Virtual objects have all of above features in general (some lack some features).
  • Virtual objects are much more precise in their name, borders, interactions, etc.

    Examples of Virtual Objects

    Bank Account
    Keeps balance; responds to messages to deposit, withdraw, and get balance
    Set
    Elements can be added, deleted, queried for presence.
    GUI Window
    Ultimately responsible for keeping contents of window. Many operations possible but include resize, move, etc.
    Conclusion: Object-oriented programming (OOP) is good because
    1. It jibes well with our intuitions: objects inside the computer correspond to real-world objects
    2. All programming problems begin in the real world (e.g., for Amazon.com there are invoices, orders, inventory, etc which all exist as objects in the real world) and so as a start on programming, can directly map real-world objects to virtual objects.

    Simple Virtual Object Example: a Set

    The external view (signature): aSet understands the messages
    1. aSet add: anElement
    2. aSet remove: anElement
    3. aSet contains: anElement
    4. aSet size
    5. aSet do: [ someCode] -runs someCode on each set member
    Messages have both an effect (internal changes or induced messages) and a return value.
    Users only need external view of aSet to use it.
    Encapsulation protects internals from view.

    Internal view:

    1. instance variables for aSet: hashArray, valueArray
      instance variables are
      1. private (generally)
      2. for just that object and
      3. have indefinate extent
    2. for each message, a method to perform the action

    Question:What properties of real-world objects does this set have?
    Definition: an object-based programming language is one which easily supports virtual objects (they can be hacked up in any language using the above formula).

    Objects and Large Software Systems

    Object view in software design is Object-oriented Design
    1. Makes code more intuitively understandable (thesis)
    2. Unifies design and programming method: initial program thoughts start from the real-world objects involved in the programming problem (invoices for account, computer gates for digital logic simulation) descriptions, even when using a non-OO language.
    3. Divides code into logical chunks individuals of a team can be experts in, and outsiders can understand via interfaces
    4. Allows "off the shelf" code libraries to be reused
    5. Supports code evolution: internals can always be re-written as long as interface stays same

    The Brave Future

    Purely virtual objects. Applications don't start from real-world objects but objects that are already virtual.
    Example: Electronic invoice payment app starts with electronic invoice.

    Classes

    Classes serve two distinct purposes

    Classes as Factories

    Recall real-world object properties: a means for creating objects.

    Making objects from class templates

    aSet := Set new "Set is the class; this makes an object"
    

    Classes and Classification

    Other main concept behind classes.

    Classification: an object can be "of" a certain class not its creator. In this view, classes are like sets.

    Mammal is an abstract class: only for classification (and inheritance).