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
- Better types for object-oriented languages
- Better (faster) compilers for object-oriented languages by
using type information
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
- PRECISE: language must give unambiguous instructions to computer
- COMPLETE: nothing the computer is capable of doing that
can't be easily expressed in the language
- FAST: duuuuh.
- HUMAN-FRIENDLY: its a language amenable to human, not machine instruction
- Highly structured: programs divided into many pieces that
may be treated independently and fit together precisely. Good
for team programming.
- Highly intuitive: Programs written in a natural way of thinking
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++
- Java
- Smalltalk
- Objective C
- CLOS
- Modula-3
- Self
C++
+'s
- most widely used
- extends C so scales up nicely
- has a type system
- relatively fast execution
-'s
- easy to not be object-oriented: can ignore all O-O features
- easy to write bad hackish code: too low level (pointers, malloc/free)
- syntax is overly complex
Smalltalk
+'s
- very high-level, don't worry about managing memory or pointers
- very flexible, can even change code while program running
- everything is an object, so it forces object view
- language comes with a large, standardized library
-'s
Java
between C++ and Smalltalk
- Slower than C++, faster than Smalltalk
- Does not extend C, like Smalltalk
- Typed
- More high-level than C++ (no pointers) but less high-level than Smalltalk
- Cleaner syntax than C++
- Pretty decent standardized library but not like Smalltalk
- Has built-in security features to allow downloading of code:
more appropriate for internet programming.
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
- It jibes well with our intuitions: objects inside the computer
correspond to real-world objects
- 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
-
aSet add: anElement
-
aSet remove: anElement
-
aSet contains: anElement
-
aSet size
-
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:
- instance variables for
aSet:
hashArray, valueArray
instance variables are
- private (generally)
- for just that object and
- have indefinate extent
- 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
- Makes code more intuitively understandable (thesis)
- 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.
- Divides code into logical chunks individuals of a team can be
experts in, and outsiders can understand via interfaces
- Allows "off the shelf" code libraries to be reused
- 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
- Factories that create new objects (code plus specification)
- Classifications of objects (specification only:
e.g.
Sizable class is any object with a
size method)
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.
- John is a man.
- Mary is a woman.
- Bob is a dog.
- All men are people.
- All women are people.
- All people are mammals.
- All dogs are mammals.
Mammal is an abstract
class: only for
classification (and inheritance).