Upload
adil-raja
View
105
Download
0
Embed Size (px)
Citation preview
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
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
Introduction Static vs Dynamic Object Creation Memory Errors Constructors Metaclasses Summary References
INTRODUCTION I
In the previous lesson we studied the static, or compile-timeaspects of classes. In this lesson we shall study their run-timefeatures.
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
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 thereceiver.
The action performed in response is determined by thereceiver, different receivers can do different actions inresponse to the same message.
Muhammad Adil Raja Roaming Researchers, Inc. cbnaMessages, Instances and Initialization
Introduction Static vs Dynamic Object Creation Memory Errors Constructors Metaclasses Summary References
MESSAGE PASSING SYNTAX I
Although the syntax may differ in different langauges, allmessages have three identifiable parts:
MESSAGE SYNTAX
aGame. d isp layCard ( aCard , 42 , 27 ) ;
The message receiver.The message selector.An optional list of arguments.
Muhammad Adil Raja Roaming Researchers, Inc. cbnaMessages, Instances and Initialization
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 todeclare a type for each variable.The validity of a message passing expression will bechecked at compile time, based on the declared type of thereceiver.A dynamically typed language associates types withvalues, not with variables.A variable is just a name.The legality of a message cannot be determined untilrun-time.
Muhammad Adil Raja Roaming Researchers, Inc. cbnaMessages, Instances and Initialization
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 apseudo-variable.
Called this in Java, C++, C#.Called self in Smalltalk, Objective-C, Object Pascal.Called current in Eiffel.
RECEIVER
f u n c t i o n PlayingCard . co l o r : co lo rs ;begin
i f ( s e l f . s u i t = Heart ) or ( s e l f . s u i t = Diamond ) thenco lo r := Red
elseco lo r := Black ;
end
Muhammad Adil Raja Roaming Researchers, Inc. cbnaMessages, Instances and Initialization
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 withno 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 ( ) { th is . setFaceUp ( ! th is . faceUp ) ; }. . .
}
Muhammad Adil Raja Roaming Researchers, Inc. cbnaMessages, Instances and Initialization
Introduction Static vs Dynamic Object Creation Memory Errors Constructors Metaclasses Summary References
OBJECT CREATION I
In most programming languages objects must be createddynamically, usually using the new operator:
EXAMPLE OF USAGE OF NEW
PlayingCard aCard ; / / s imply names a new v a r i a b l e
aCard = new PlayingCard ( Diamond , 3 ) ; / / c reates the new ob jec t
The declaration simply names a variable, the new operator isneeded to create the new object value.
Muhammad Adil Raja Roaming Researchers, Inc. cbnaMessages, Instances and Initialization
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 broadapproches to this:
Force the programmer to explicitly say when a value is nolonger being used:
EXAMPLE OF DELETE
delete aCard ; / / C++ example
Use a garbage collection system that will automaticallydetermine when values are no longer being used, andrecover the memory.
Muhammad Adil Raja Roaming Researchers, Inc. cbnaMessages, Instances and Initialization
Introduction Static vs Dynamic Object Creation Memory Errors Constructors Metaclasses Summary References
MEMORY ERRORS I
Garbage collection systems impose a run-time overhead, butprevent a number of potential memory errors:
Running out of memory because the programmer forgot tofree valuesUsing 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
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 ; / / de le te a l ready dele ted value
Muhammad Adil Raja Roaming Researchers, Inc. cbnaMessages, Instances and Initialization
Introduction Static vs Dynamic Object Creation Memory Errors Constructors Metaclasses Summary References
CONSTRUCTORS I
A constructor is a function that is implicitly invoked when anew object is created.The constructor performs whatever actions are necessaryin order to initialize the object.In C++, Java, C# a constructor is a function with the samename 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
Introduction Static vs Dynamic Object Creation Memory Errors Constructors Metaclasses Summary References
CONSTRUCTORS II
CONSTRUCTOR
class PlayingCard { / / a Java cons t ruc to rpublic PlayingCard ( i n t s , i n t r ) {
s u i t = s ;rank = r ;faceUp = true ;
}. . .
}
Muhammad Adil Raja Roaming Researchers, Inc. cbnaMessages, Instances and Initialization
Introduction Static vs Dynamic Object Creation Memory Errors Constructors Metaclasses Summary References
OVERLOADED CONSTRUCTORS I
Constructors are often overloaded, meaning there are anumber of functions with the same name.
They are differentiated by the type signature, and thearguments used in the function call or declaration:
OVERLOADED CONSTRUCTOR
Muhammad Adil Raja Roaming Researchers, Inc. cbnaMessages, Instances and Initialization
Introduction Static vs Dynamic Object Creation Memory Errors Constructors Metaclasses Summary References
OVERLOADED CONSTRUCTORS II
class PlayingCard {public :
PlayingCard ( ) / / d e f a u l t cons t ruc to r ,/ / used when no arguments are given
{ s u i t = Diamond ; rank = 1; faceUp = true ; }
PlayingCard ( Su i t i s ) / / cons t r uc to r w i th one argument{ s u i t = i s ; rank = 1; faceUp = true ; }
PlayingCard ( Su i t i s , i n t i r ) / / cons t r uc to r w i th two arguments{ s u i t = i s ; rank = i r ; faceUp = true ; }
} ;
Muhammad Adil Raja Roaming Researchers, Inc. cbnaMessages, Instances and Initialization
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 ofinstances of the class, since we don’t yet have an instance.They can’t be part of the messages understood by classClass, since not all classes have the same constructormessage.Where do we put the behavior for individual classinstances?
Muhammad Adil Raja Roaming Researchers, Inc. cbnaMessages, Instances and Initialization
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 isitself a class. picture
An elegant solution that maintains the simpleinstance/class relationship.
Muhammad Adil Raja Roaming Researchers, Inc. cbnaMessages, Instances and Initialization
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
Introduction Static vs Dynamic Object Creation Memory Errors Constructors Metaclasses Summary References
REFERENCES I
Images and content for developing these slides have beentaken from the follwoing book.
An Introduction to Object Oriented Programming, TimothyBudd.This presentation is developed using Beamer:
Berlin, monarca.
Muhammad Adil Raja Roaming Researchers, Inc. cbnaMessages, Instances and Initialization