CSci 581: Obj.-Oriented Design & Programming
Spring Semester 1999
Lecture Notes


Implementing Objects with Classes

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).


Encapsulation and Instantiation

Classes provide two very important capabilities:

Encapsulation --
purposeful hiding of information, thereby reducing the details that must be remembered and communicated

Instantiation --
creation of multiple instances of an abstraction

Java's package facility provides a larger-grained encapsulation mechanism


Encapsulation: Parnas's Principles

To achieve Parnas's principles, keep the interface (public face) of a class separate from the implementation (private face) of the class.


Object State -- Instance Variables


Example -- Playing Card

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.

Card
Maintain and return suit and rank
Return color of face
Maintain face-up and face-down status
Draw card on playing surface
Erase card from playing surface
Collaborators


Playing Card Back -- Data Areas

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.

Card
int suitValue
0 = Heart, 1 = Club,
2 = Diamond, 3 = Spade
int rankValue -- 1 through 12
boolean faceup


Refinement of Playing Card

We refine the behavior by providing explicit function names and argument types.

Card
suit() -- return suit value
rank() -- return rank value
color() -- return color value
erase(x,y) -- erase card image
draw(x,y) -- draw card image
faceUp(), flip() -- test or flip card
Collaborators


Interface and Implementation

Distinction between interface and implementation is manifest in different ways:


Varieties of Classes

Data manager
principal responsibility is to maintain data values

Example: Playing card -- maintains suit and value information

Data sink or source
interface to data generator or consumer, no data itself

Example: Input/Output stream objects - files, I/O devices

View or observer class
display objects graphically, not the object itself

Facilitator or helper class
maintain little/no state, but assist with complex tasks

Example: painting assistant (Pen, Canvas, Edit box) for graphics output device


Classes and Methods in Java

Java is similar to C++, but


Playing Card in Java

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;
}


Storage Allocation

How is space allocated to variables?

Static allocation
-- uses declaration statements
-- allocates variables before execution

Example: global variables in Fortran or C

Automatic allocation (sometimes called semidynamic allocation)
-- uses declaration statements
-- automatically allocates variables on entering scope
-- stack-based storage management

Example: local variables in Pascal or C

Dynamic allocation
-- often uses explicit directives
-- sometimes implicit as side-effect of other operations
-- heap-based storage management

Example: explicit new in Java and C++

Example: implicit side-effect of string concatenation in Java


Storage Deallocation

Explicit deallocation by programmer
-- programmer must return unneeded memory to system

Examples: C++ delete, Pascal dispose

Advantage: efficiency

Disadvantages:

Implicit deallocation by runtime system
-- system detects when data unneeded, automatically recovers memory
-- garbage collection

Examples: Java, Smalltalk, Perl

Advantage: safety and convenience

Disadvantage: relative inefficiency / loss of programmer control


Java Storage Management


Object Creation and Initialization

Many OOP languages link object creation and initialization to:

A constructor is a method that:


Constructors in Java

A constructor in Java (and C++) is a method that:

   
   class newClass {
      ...
      newClass (int i) {
         ... // initialization
      }
   }


Object Deletion and Reclamation

A destructor is a method that:

In languages with explicit deallocation (e.g., C++), programmer-defined destructors reclaim object storage and other resources.


Destructors in Java


Message Passing

Message-passing:

Message passing differs from a simple function call:


Message Passing Syntax


UP to CSCI 581 Lecture Notes root document?


Copyright © 1999, H. Conrad Cunningham
Last modified: Wed Apr 21 16:02:00 CDT