1. Introduction Static vs Dynamic Object Creation Memory Errors Constructors Metaclasses Summary References
MESSAGES, INSTANCES AND INITIALIZATION
Muhammad Adil Raja
Roaming Researchers, Inc.
cbna
April 15, 2015
Muhammad Adil Raja Roaming Researchers, Inc. cbnaMessages, Instances and Initialization
2. Introduction Static vs Dynamic Object Creation Memory Errors Constructors Metaclasses Summary References
OUTLINE I
1 INTRODUCTION
2 STATIC VS DYNAMIC
3 OBJECT CREATION
4 MEMORY ERRORS
5 CONSTRUCTORS
6 METACLASSES
7 SUMMARY
8 REFERENCES
Muhammad Adil Raja Roaming Researchers, Inc. cbnaMessages, Instances and Initialization
3. Introduction Static vs Dynamic Object Creation Memory Errors Constructors Metaclasses Summary References
INTRODUCTION I
In the previous lesson we studied the static, or compile-time
aspects of classes. In this lesson we shall study their run-time
features.
Message Passing Syntax.
Object Creation and Initialization (constructors).
Accessing the Receiver from within a method.
Memory Management or garbage collection.
Muhammad Adil Raja Roaming Researchers, Inc. cbnaMessages, Instances and Initialization
4. Introduction Static vs Dynamic Object Creation Memory Errors Constructors Metaclasses Summary References
MESSAGES ARE NOT FUNCTION CALLS I
Difference between a message and a function 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.
Muhammad Adil Raja Roaming Researchers, Inc. cbnaMessages, Instances and Initialization
5. Introduction Static vs Dynamic Object Creation Memory Errors Constructors Metaclasses Summary References
MESSAGE PASSING SYNTAX I
Although the syntax may differ in different langauges, all
messages have three identifiable parts:
MESSAGE SYNTAX
aGame. displayCard ( aCard , 42 , 27);
The message receiver.
The message selector.
An optional list of arguments.
Muhammad Adil Raja Roaming Researchers, Inc. cbnaMessages, Instances and Initialization
6. Introduction Static vs Dynamic Object Creation Memory Errors Constructors Metaclasses Summary References
STATICALLY TYPED VERSUS DYNAMICALLY TYPED
LANGUAGES I
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.
Muhammad Adil Raja Roaming Researchers, Inc. cbnaMessages, Instances and Initialization
7. Introduction Static vs Dynamic Object Creation Memory Errors Constructors Metaclasses Summary References
THE RECEIVER VARIABLE I
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.
RECEIVER
function PlayingCard . color : colors ;
begin
i f ( s e l f . s u i t = Heart ) or ( s e l f . s u i t = Diamond ) then
color := Red
else
color := Black ;
end
Muhammad Adil Raja Roaming Researchers, Inc. cbnaMessages, Instances and Initialization
8. Introduction Static vs Dynamic Object Creation Memory Errors Constructors Metaclasses Summary References
IMPLICIT USE OF THIS I
Within a method a message expression or a data access with
no explicit receiver is implicitly assumed to refer to this:
RECEIVER
class PlayingCard {
. . .
public void f l i p ( ) { setFaceUp ( ! faceUp ) ; }
. . .
}
Is equivalent to:
class PlayingCard {
. . .
public void f l i p ( ) { this . setFaceUp ( ! this . faceUp ) ; }
. . .
}
Muhammad Adil Raja Roaming Researchers, Inc. cbnaMessages, Instances and Initialization
9. Introduction Static vs Dynamic Object Creation Memory Errors Constructors Metaclasses Summary References
OBJECT CREATION I
In most programming languages objects must be created
dynamically, usually using the new operator:
EXAMPLE OF USAGE OF NEW
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.
Muhammad Adil Raja Roaming Researchers, Inc. cbnaMessages, Instances and Initialization
10. Introduction Static vs Dynamic Object Creation Memory Errors Constructors Metaclasses Summary References
MEMORY RECOVERY I
Because in most languages objects are dynamically allocated,
they must be recovered at run-time. There are two broad
approches to this:
Force the programmer to explicitly say when a value is no
longer being used:
EXAMPLE OF DELETE
delete aCard ; / / C++ example
Use a garbage collection system that will automatically
determine when values are no longer being used, and
recover the memory.
Muhammad Adil Raja Roaming Researchers, Inc. cbnaMessages, Instances and Initialization
11. Introduction Static vs Dynamic Object Creation Memory Errors Constructors Metaclasses Summary References
MEMORY ERRORS I
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.
MORE DELETE
PlayingCard ∗ aCard = new PlayingCard (Spade , 1 ) ;
delete aCard ;
cout << aCard . rank ( ) ;
Free the same value twice.
Muhammad Adil Raja Roaming Researchers, Inc. cbnaMessages, Instances and Initialization
12. Introduction Static vs Dynamic Object Creation Memory Errors Constructors Metaclasses Summary References
MEMORY ERRORS II
DELETE CONTINUED
PlayingCard ∗ aCard = new PlayingCard (Spade , 1 ) ;
delete aCard ;
delete aCard ; / / delete already deleted value
Muhammad Adil Raja Roaming Researchers, Inc. cbnaMessages, Instances and Initialization
13. Introduction Static vs Dynamic Object Creation Memory Errors Constructors Metaclasses Summary References
CONSTRUCTORS I
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.
Muhammad Adil Raja Roaming Researchers, Inc. cbnaMessages, Instances and Initialization
14. Introduction Static vs Dynamic Object Creation Memory Errors Constructors Metaclasses Summary References
CONSTRUCTORS II
CONSTRUCTOR
class PlayingCard { / / a Java constructor
public PlayingCard ( int s , int r ) {
s u i t = s ;
rank = r ;
faceUp = true ;
}
. . .
}
Muhammad Adil Raja Roaming Researchers, Inc. cbnaMessages, Instances and Initialization
15. Introduction Static vs Dynamic Object Creation Memory Errors Constructors Metaclasses Summary References
OVERLOADED CONSTRUCTORS I
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:
OVERLOADED CONSTRUCTOR
Muhammad Adil Raja Roaming Researchers, Inc. cbnaMessages, Instances and Initialization
16. Introduction Static vs Dynamic Object Creation Memory Errors Constructors Metaclasses Summary References
OVERLOADED CONSTRUCTORS II
class PlayingCard {
public :
PlayingCard ( ) / / default constructor ,
/ / used when no arguments are given
{ s u i t = Diamond ; rank = 1; faceUp = true ; }
PlayingCard ( Suit i s ) / / constructor with one argument
{ s u i t = i s ; rank = 1; faceUp = true ; }
PlayingCard ( Suit is , int i r ) / / constructor with two arguments
{ s u i t = i s ; rank = i r ; faceUp = true ; }
} ;
Muhammad Adil Raja Roaming Researchers, Inc. cbnaMessages, Instances and Initialization
17. Introduction Static vs Dynamic Object Creation Memory Errors Constructors Metaclasses Summary References
METACLASSES I
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?
Muhammad Adil Raja Roaming Researchers, Inc. cbnaMessages, Instances and Initialization
18. Introduction Static vs Dynamic Object Creation Memory Errors Constructors Metaclasses Summary References
METACLASSES II
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.
Muhammad Adil Raja Roaming Researchers, Inc. cbnaMessages, Instances and Initialization
19. Introduction Static vs Dynamic Object Creation Memory Errors Constructors Metaclasses Summary References
SUMMARY I
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.
Muhammad Adil Raja Roaming Researchers, Inc. cbnaMessages, Instances and Initialization
20. Introduction Static vs Dynamic Object Creation Memory Errors Constructors Metaclasses Summary References
REFERENCES I
Images and content for developing these slides have been
taken from the follwoing book.
An Introduction to Object Oriented Programming, Timothy
Budd.
This presentation is developed using Beamer:
Berlin, monarca.
Muhammad Adil Raja Roaming Researchers, Inc. cbnaMessages, Instances and Initialization