SlideShare a Scribd company logo
1 of 20
Download to read offline
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-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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 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

More Related Content

What's hot

Understanding Reflection
Understanding ReflectionUnderstanding Reflection
Understanding Reflection
Tamir Khason
 
Extreme Interview Questions
Extreme Interview QuestionsExtreme Interview Questions
Extreme Interview Questions
Ehtisham Ali
 
2. oop with c++ get 410 day 2
2. oop with c++ get 410   day 22. oop with c++ get 410   day 2
2. oop with c++ get 410 day 2
Mukul kumar Neal
 
Java session05
Java session05Java session05
Java session05
Niit Care
 

What's hot (20)

Object-oriented Basics
Object-oriented BasicsObject-oriented Basics
Object-oriented Basics
 
Delphi qa
Delphi qaDelphi qa
Delphi qa
 
Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes
 
JavaScript Programming
JavaScript ProgrammingJavaScript Programming
JavaScript Programming
 
Design patterns illustrated-2015-03
Design patterns illustrated-2015-03Design patterns illustrated-2015-03
Design patterns illustrated-2015-03
 
Lambda: A Peek Under The Hood - Brian Goetz
Lambda: A Peek Under The Hood - Brian GoetzLambda: A Peek Under The Hood - Brian Goetz
Lambda: A Peek Under The Hood - Brian Goetz
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Sdtl manual
Sdtl manualSdtl manual
Sdtl manual
 
CORBA IDL
CORBA IDLCORBA IDL
CORBA IDL
 
Understanding Reflection
Understanding ReflectionUnderstanding Reflection
Understanding Reflection
 
Extreme Interview Questions
Extreme Interview QuestionsExtreme Interview Questions
Extreme Interview Questions
 
Top 20 c# interview Question and answers
Top 20 c# interview Question and answersTop 20 c# interview Question and answers
Top 20 c# interview Question and answers
 
Core java Basics
Core java BasicsCore java Basics
Core java Basics
 
2. oop with c++ get 410 day 2
2. oop with c++ get 410   day 22. oop with c++ get 410   day 2
2. oop with c++ get 410 day 2
 
Java Programming | Java Tutorial For Beginners | Java Training | Edureka
Java Programming | Java Tutorial For Beginners | Java Training | EdurekaJava Programming | Java Tutorial For Beginners | Java Training | Edureka
Java Programming | Java Tutorial For Beginners | Java Training | Edureka
 
Moved to https://slidr.io/azzazzel/osgi-fundamentals
Moved to https://slidr.io/azzazzel/osgi-fundamentalsMoved to https://slidr.io/azzazzel/osgi-fundamentals
Moved to https://slidr.io/azzazzel/osgi-fundamentals
 
Java mcq
Java mcqJava mcq
Java mcq
 
50+ java interview questions
50+ java interview questions50+ java interview questions
50+ java interview questions
 
Java session05
Java session05Java session05
Java session05
 
Condor overview - glideinWMS Training Jan 2012
Condor overview - glideinWMS Training Jan 2012Condor overview - glideinWMS Training Jan 2012
Condor overview - glideinWMS Training Jan 2012
 

Similar to Messages, Instances and Initialization

ActionScript 3.0 Fundamentals
ActionScript 3.0 FundamentalsActionScript 3.0 Fundamentals
ActionScript 3.0 Fundamentals
Saurabh Narula
 
Javascriptinobject orientedway-090512225827-phpapp02
Javascriptinobject orientedway-090512225827-phpapp02Javascriptinobject orientedway-090512225827-phpapp02
Javascriptinobject orientedway-090512225827-phpapp02
Sopheak Sem
 
Smoothing Your Java with DSLs
Smoothing Your Java with DSLsSmoothing Your Java with DSLs
Smoothing Your Java with DSLs
intelliyole
 

Similar to Messages, Instances and Initialization (20)

Core java-course-content
Core java-course-contentCore java-course-content
Core java-course-content
 
Core java-training-course-content
Core java-training-course-contentCore java-training-course-content
Core java-training-course-content
 
Core java-course-content
Core java-course-contentCore java-course-content
Core java-course-content
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Jvm internals
Jvm internalsJvm internals
Jvm internals
 
Java questions with answers
Java questions with answersJava questions with answers
Java questions with answers
 
Bca5020 visual programming
Bca5020  visual programmingBca5020  visual programming
Bca5020 visual programming
 
Bca5020 visual programming
Bca5020  visual programmingBca5020  visual programming
Bca5020 visual programming
 
Oopp Lab Work
Oopp Lab WorkOopp Lab Work
Oopp Lab Work
 
Dynamic Language Performance
Dynamic Language PerformanceDynamic Language Performance
Dynamic Language Performance
 
Module 10 : creating and destroying objects
Module 10 : creating and destroying objectsModule 10 : creating and destroying objects
Module 10 : creating and destroying objects
 
ActionScript 3.0 Fundamentals
ActionScript 3.0 FundamentalsActionScript 3.0 Fundamentals
ActionScript 3.0 Fundamentals
 
Javascriptinobject orientedway-090512225827-phpapp02
Javascriptinobject orientedway-090512225827-phpapp02Javascriptinobject orientedway-090512225827-phpapp02
Javascriptinobject orientedway-090512225827-phpapp02
 
Generation_XSD_Article.docx
Generation_XSD_Article.docxGeneration_XSD_Article.docx
Generation_XSD_Article.docx
 
JavaScript Miller Columns
JavaScript Miller ColumnsJavaScript Miller Columns
JavaScript Miller Columns
 
C# features
C# featuresC# features
C# features
 
Ch03
Ch03Ch03
Ch03
 
Ch03
Ch03Ch03
Ch03
 
Smoothing Your Java with DSLs
Smoothing Your Java with DSLsSmoothing Your Java with DSLs
Smoothing Your Java with DSLs
 
Drilling the Async Library
Drilling the Async LibraryDrilling the Async Library
Drilling the Async Library
 

More from adil raja

More from adil raja (20)

ANNs.pdf
ANNs.pdfANNs.pdf
ANNs.pdf
 
A Software Requirements Specification
A Software Requirements SpecificationA Software Requirements Specification
A Software Requirements Specification
 
NUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehicles
NUAV - A Testbed for Development of Autonomous Unmanned Aerial VehiclesNUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehicles
NUAV - A Testbed for Development of Autonomous Unmanned Aerial Vehicles
 
DevOps Demystified
DevOps DemystifiedDevOps Demystified
DevOps Demystified
 
On Research (And Development)
On Research (And Development)On Research (And Development)
On Research (And Development)
 
Simulators as Drivers of Cutting Edge Research
Simulators as Drivers of Cutting Edge ResearchSimulators as Drivers of Cutting Edge Research
Simulators as Drivers of Cutting Edge Research
 
The Knock Knock Protocol
The Knock Knock ProtocolThe Knock Knock Protocol
The Knock Knock Protocol
 
File Transfer Through Sockets
File Transfer Through SocketsFile Transfer Through Sockets
File Transfer Through Sockets
 
Remote Command Execution
Remote Command ExecutionRemote Command Execution
Remote Command Execution
 
Thesis
ThesisThesis
Thesis
 
CMM Level 3 Assessment of Xavor Pakistan
CMM Level 3 Assessment of Xavor PakistanCMM Level 3 Assessment of Xavor Pakistan
CMM Level 3 Assessment of Xavor Pakistan
 
Data Warehousing
Data WarehousingData Warehousing
Data Warehousing
 
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
 
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
Implementation of a Non-Intrusive Speech Quality Assessment Tool on a Mid-Net...
 
Real-Time Non-Intrusive Speech Quality Estimation for VoIP
Real-Time Non-Intrusive Speech Quality Estimation for VoIPReal-Time Non-Intrusive Speech Quality Estimation for VoIP
Real-Time Non-Intrusive Speech Quality Estimation for VoIP
 
VoIP
VoIPVoIP
VoIP
 
ULMAN GUI Specifications
ULMAN GUI SpecificationsULMAN GUI Specifications
ULMAN GUI Specifications
 
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
 
ULMAN-GUI
ULMAN-GUIULMAN-GUI
ULMAN-GUI
 
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
Modeling the Effect of Packet Loss on Speech Quality: Genetic Programming Bas...
 

Recently uploaded

Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 

Recently uploaded (20)

Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 

Messages, Instances and Initialization

  • 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