[first slide]

Introduction to Object Oriented Programming, 3rd Ed

Timothy A. Budd

Chapter 5

Messages, Instances and Initialization

Outline

  1. Roadmap
  2. Messages are not Function Calls
  3. Message Passing Syntax
  4. Statically and Dynamically Typed Languages
  5. The Receiver Variable
    1. Implicit Use of This
  6. Object Creation
  7. Memory Recovery
    1. Memory Errors
  8. Constructors
    1. Overloaded Constructors
  9. Metaclasses
    1. New Classes for Classes
  10. Chapter Summary

Other Material

Intro OOP, Chapter 3, Outline

Roadmap

In Chapter 4 we described the static, or compile time aspects of classes. In this chapter we examine their run-time features:
Intro OOP, Chapter 5, Slide 01

Messages are not Function Calls

Recall from chapter 1 that we noted the following differences between a message and a function (or procedure) call
Intro OOP, Chapter 5, Slide 02

Message Passing Syntax

Although the syntax may differ in different langauges, all messages have three identifiable parts:
	aGame.displayCard (aCard, 42, 27);

Intro OOP, Chapter 5, Slide 03

Statically Types and Dynamically Typed Languages

A distinction we will see throughout the term is between the following:
Intro OOP, Chapter 5, Slide 04

The Receiver Variable

Inside a method, the receiver can be accessed by means of a pseudo-variable

function PlayingCard.color : colors;
begin
	if (self.suit = Heart) or (self.suit = Diamond) then
		color := Red
	else
		color := Black;
end

Intro OOP, Chapter 5, Slide 05

Implicit Use of This

Within a method a message expression or a data access with no explicit receiver is implicitly assumed to refer to this:

class PlayingCard {
	...
	public void flip () { setFaceUp( ! faceUp ); }
	...
}

Is assumed to be equivalent to:

class PlayingCard {
	...
	public void flip () { this.setFaceUp( ! this.faceUp); }
	...
}

Intro OOP, Chapter 5, Slide 06

Object Creation

In most programming languages objects must be created dynamically, usually using the new operator:

	PlayingCard aCard; // simply names a new variable

	aCard = new PlayingCard(Diamond, 3); // creates the new object

The declaration simply names a variable, the new operator is needed to create the new object value.
Intro OOP, Chapter 5, Slide 07

Memory Recovery

Because in most languages objects are dynamically allocated, they must be recovered at run-time. There are two broad approches to this:
Intro OOP, Chapter 5, Slide 08

Memory Errors

Garbage collection systems impose a run-time overhead, but prevent a number of potential memory errors:
Intro OOP, Chapter 5, Slide 09

Constructors

A constructor is a function that is implicitly invoked when a new object is created. The constructor performs whatever actions are necessary in order to initialize the object.

class PlayingCard {	// a Java constructor
	public PlayingCard (int s, int r) {
		suit = s;
		rank = r;
		faceUp = true;
	}
	...
}

Intro OOP, Chapter 5, Slide 10

Overloaded Constructors

Constructors are often overloaded, meaning there are a number of functions with the same name. They are differentiated by the type signature, and the arguments used in the function call or declaration:

class PlayingCard {
public:
	PlayingCard ( )  // default constructor, 
			// used when no arguments are given
		{ suit = Diamond; rank = 1; faceUp = true; }

	PlayingCard (Suit is) // constructor with one argument
		{ suit = is; rank = 1; faceUp = true; }

	PlayingCard (Suit is, int ir) // constructor with two arguments
		{ suit = is; rank = ir; faceUp = true; }
};

Intro OOP, Chapter 5, Slide 11

Metaclasses

In Smalltalk (and Objective-C) classes are just objects, instances of class Class. new is just a message given to a class object. If we want to create constructors, where do we put them? Where do we put the behavior for individual class instances?
Intro OOP, Chapter 5, Slide 12

Solution: Create New Classes for Classes

The solution is to create a new class, whos only instance is itself a class.

picture

An elegant solution that maintains the simple instance/class relationship.

Intro OOP, Chapter 5, Slide 13

Chapter Summary

In this chapter we have examined the following topics:
Intro OOP, Chapter 5, Slide 14