This Web document was based on a set of "slides" created by the instructor for use in the Fall 1997 offering of the object-oriented design and programming course. That set was, in turn, based on a set of slides created by Timothy Budd to supplement chapters 3 and 4 of his textbook An Introduction to Object-Oriented Programming, Second Edition (Addison-Wesley, 1997).
Classes provide two very important capabilities:
Java's package
facility provides a larger-grained
encapsulation mechanism
To achieve Parnas's principles, keep the interface (public face) of a class separate from the implementation (private face) of the class.
Below illustrates the front side of a CRC card for a playing card class.
Think of the front side of a card as being its public face -- the interface.
|
|
The back can record information about the internal state of an instance.
Think of the back of the card being its private face -- the implementation.
|
We refine the behavior by providing explicit function names and argument types.
|
|
Distinction between interface and implementation is manifest in different ways:
Example: public
and private
features in Java
and C++
Example: Java interface
definition and class
that implements
it
Example: C++ .h
and *.cpp
files
Example: Playing card -- maintains suit and value information
Example: Input/Output stream objects - files, I/O devices
Example: painting assistant (Pen, Canvas, Edit box) for graphics output device
Java is similar to C++, but
static final
variables)
public
and private
applied
individually to features
public class Card { // static values for colors static final public int red = 0; static final public int black = 1; // static values for suits static final public int spade = 0; static final public int heart = 1; static final public int diamond = 2; static final public int club = 3; // constructor public Card (int sv, int rv) { suitValue = sv; rankValue = rv; faceup = false; } // mutators public void flip() { faceup = ! faceup; } // accessors public int suit() { return suitValue; } public int rank() { return rankValue; } public int color () { if (suit() == heart || suit() == diamond) return red; return black ; } public boolean faceUp() { return faceup; } // perform other actions public void draw (Graphics g, int x, int y) { ... /* omitted */ ... } // data fields private boolean faceup; private int rankValue; private int suitValue; }
How is space allocated to variables?
Example: global variables in Fortran or C
Example: local variables in Pascal or C
Example: explicit new
in Java and C++
Example: implicit side-effect of string concatenation in Java
Examples: C++ delete
, Pascal dispose
Advantage: efficiency
Disadvantages:
Examples: Java, Smalltalk, Perl
Advantage: safety and convenience
Disadvantage: relative inefficiency / loss of programmer control
static
) variables upon
initial loading of class
Local variables only contain primitive values or references to class instances
int count; Card aCard;
aCard = new Card(Card.spade,5);
Many OOP languages link object creation and initialization to:
A constructor is a method that:
A constructor in Java (and C++) is a method that:
new
, explicitly by
this
in other constructors
class newClass { ... newClass (int i) { ... // initialization } }
A destructor is a method that:
In languages with explicit deallocation (e.g., C++), programmer-defined destructors reclaim object storage and other resources.
finalize()
, if defined, called when
reclaiming memory for object
Message-passing:
Message passing differs from a simple function call:
this
in
Java and C++
Below Card
instance aCard
is implicit
argument of flip
and draw
Card aCard: aCard = new Card( ... ); aCard.flip(); aCard.draw(x,y);
UP to CSCI 581 Lecture Notes root document?