Inheritance and Lookup
2: Lookup
Damien Cassou, Stéphane Ducasse and Luc Fabresse
http://stephane.ducasse.free.fr
Goal
- Understanding
- message sending
- method lookup
- semantics of
self
Inheritance
- Inheritance of state is static
- Inheritance of behavior is dynamic
Message Sending
Sending a message is a two-step process:
- look up the method matching the message
- execute this method on the receiver
Method Lookup
The lookup starts in the class of the receiver then:
- if the method is defined in the class, it is returned
- otherwise the search continues in the superclass
Some Lookup Cases
Sending the message color
to aColoredRectangle
Some Lookup Cases
Sending the message area
to aColoredRectangle
self Always Represents the Receiver
self Always Represents the Receiver
What is self/this?
Take 5 min and write the definition of self
(this
in Java).
- your definition should have two points:
- what does
self
represent?
- how is a method looked up when a message is sent to
self
?
self/this
self
represents the receiver of the message
self
in Pharo, this
in Java
- The method lookup starts in the class of the receiver
self Always Represents the Receiver
self Always Represents the Receiver
self Always Represents the Receiver
Evaluation of aB bar
aB
's class is B
- no method
bar
in B
- look up in
A
- bar
is found
- method
bar
is executed
self
refers to the receiver aB
foo
is sent to self
- look up
foo
in the aB
's class: B
foo
is found there and executed
self Always Represents the Receiver
self Always Represents the Receiver
What You Should Know
self
always represents the receiver
- Sending a message is a two-step process:
- Look up the method matching the message
- Execute this method on the receiver
- Method lookup maps a message to a method
- Method lookup starts in the class of the receiver
- ...and goes up in the hierarchy
/