Did You Really Understand Super?
Damien Cassou, Stéphane Ducasse and Luc Fabresse
http://stephane.ducasse.free.fr
What You Will Learn
Revisit
super
- Message lookup
- Class methods
A Little Puzzle
We execute the following expression: Dice new
Questions
- What is
inst
?
- What is
super
?
- What is
super new
?
Hint: super is Not...
- No
super
is not the superclass
- No
inst
is not an instance of the superclass
Hint 2: super is the Message Receiver
- The message is
Dice new
- So the receiver is the class
Dice
Sending a Message: Lookup + Apply on Receiver
Remember: Method Lookup
Solution
super
is the receiver: the class Dice
- Look for
new
in the superclass of the class Dice class
(Pay attention not Dice
)
- Once found we apply to the receiver:
Dice
- We get an instance of the class
Dice
and send it initialize and return it
Solution
Summary
- Sending a message is looking up for the method and applying it on the receiver
- Now you should really understand
super
:)
super
is the receiver of the message and the method lookup starts in the superclass of the class containing the expression
Challenge?
Challenge!
Imagine we have
What is the result of A new foo
and why?
/