super
semantics and the differences with self
Take 5 min and write the definition of super
super
represent?super
?
A new bar
> ...
B new bar
> ...
C new bar
> ...
A new bar
> 10
B new bar
> 20
C new bar
> 100
Evaluation of aC bar
aC
's class is C
bar
in CB
- bar
is foundbar
is executedbar
is sent to super
super
is aC
but lookup starts in A
bar
is found in A
and executedfoo
is sent to aC
foo
is found in C
super
refers to the receiver of the message (just like self
)super
expression
We don't know which foo
method bar
refers to
B>>foo
refers to A>>foo
super
super
looks for the method in the superclass of the receiver's class
In reality it does not loop, the definition is wrong
self
always represents the receiversuper
always represents the receiversuper
changes the lookup:/