[first slide]
Introduction to Object Oriented Programming, 3rd Ed
Chapter 5
Messages, Instances and Initialization
Outline
- Roadmap
- Messages are not Function Calls
- Message Passing Syntax
- Statically and Dynamically Typed Languages
- The Receiver Variable
- Implicit Use of This
- Object Creation
- Memory Recovery
- Memory Errors
- Constructors
- Overloaded Constructors
- Metaclasses
- New Classes for Classes
- 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:
- Message Passing Syntax
- Object Creation and Initialization (constructors)
- Accessing the Receiver from within a method
- Memory Management or garbage collection
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
- A message is always given to some object, called the receiver
- The action performed in response is determined by the receiver,
different receivers can do different actions in response to the same
message.
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);
- The message receiver
- The message selector
- An optional list of arguments
Intro OOP, Chapter 5, Slide 03
Statically Types and Dynamically Typed Languages
A distinction we will see throughout the term is between the following:
- A statically typed language requires the programmer to declare
a type for each variable. The validity of a message passing expression
will be checked at compile time, based on the declared type of the
receiver.
- A dynamically typed language associates types with values,
not with variables. A variable is just a name.
The legality of a message cannot be determined until run-time.
Intro OOP, Chapter 5, Slide 04
The Receiver Variable
Inside a method, the receiver can be accessed by means of a pseudo-variable
- Called this in Java, C++, C#
- Called self in Smalltalk, Objective-C, Object Pascal
- Called current in Eiffel
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:
- Running out of memory because the programmer forgot to free values
- Using a memory value after it has been recovered
PlayingCard * aCard = new PlayingCard(Spade, 1);
delete aCard;
cout << aCard.rank();
- Free the same value twice
PlayingCard * aCard = new PlayingCard(Spade, 1);
delete aCard;
delete aCard; // delete already deleted value
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.
- In C++, Java, C# a constructor is a function with the same name
as the class.
- In Python constructors are all named __init__
- In Delphi, Objective-C, constructors have special syntax,
but can be named anything. (Naming your constructors create is a
common convention).
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?
- They can't be part of the collection of messages of instances of
the class, since we don't yet have an instance.
- They can't be part of the messages understood by class Class,
since not all classes have the same constructor message.
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:
- Message Passing Syntax
- Object Creation and Initialization (constructors)
- Accessing the Receiver from within a method
- Memory Management or garbage collection
- Metaclasses in Smalltalk
Intro OOP, Chapter 5, Slide 14