S.Ducasse 1
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Stéphane Ducasse
Stephane.Ducasse@univ-savoie.fr
http://www.iam.unibe.ch/~ducasse/
Inheritance Semantics
and Method Lookup
S.Ducasse 2
Goal
• Inheritance
• Method lookup
• Self/super difference
S.Ducasse 3
Inheritance
• Do not want to rewrite everything!
• Often we want small changes
• We would like to reuse and extend existing behavior
• Solution: class inheritance
• Each class defines or refines the definition
• of its ancestors
S.Ducasse 4
Inheritance
• New classes
• Can add state and behavior:
• color, borderColor, borderWidth,
• totalArea
• Can specialize ancestor behavior
• intersect:
• Can use ancestor’s behavior and state
• Can redefine ancestor’s behavior
• area to return totalArea
S.Ducasse 5
Inheritance in Smalltalk
• Single inheritance
• Static for the instance variables
• At class creation time the instance variables are
collected from the superclasses and the class. No
repetition of instance variables.
• Dynamic for the methods
• Late binding (all virtual) methods are looked up at
run-time depending on the dynamic type of the
receiver.
S.Ducasse 6
Message Sending
• receiver selector args
• Sending a message = looking up the method that
should be executed and executing it
• Looking up a method: When a message (receiver
selector args) is sent, the method corresponding to
the message selector is looked up through the
inheritance chain.
S.Ducasse 7
Method Lookup
• Two steps process
• The lookup starts in the CLASS of the RECEIVER.
• If the method is defined in the method dictionary, it is
returned.
• Otherwise the search continues in the superclasses of
the receiver's class. If no method is found and there is
no superclass to explore (class Object), this is an
ERROR
S.Ducasse 8
Lookup: class and inheritance
S.Ducasse 9
Some Cases
S.Ducasse 10
Method Lookup starts in Receiver Class
aB foo
(1) aB class => B
(2) Is foo defined in B?
(3) Foo is executed -> 50
aB bar
(1) aB class => B
(2) Is bar defined in B?
(3) Is bar defined in A?
(4) bar executed
(5) Self class => B
(6) Is foo defined in B
(7) Foo is executed -> 50
S.Ducasse 11
self **always** represents the receiver
• A new foo
• -> 10
• B new foo
• -> 10
• C new foo
• -> 50
• A new bar
• -> 10
• B new bar
• -> 10
• C new bar
• -> 50
S.Ducasse 12
When message is not found
• If no method is found and there is no superclass to
explore (class Object), a new method called
#doesNotUnderstand: is sent to the receiver, with a
representation of the initial message.
S.Ducasse 13
Graphically…
1
2
S.Ducasse 14
…in Smalltalk
• node1 print: aPacket
– node is an instance of Node
– print: is looked up in the class Node
– print: is not defined in Node > lookup continues in Object
– print: is not defined in Object => lookup stops +
exception
– message: node1 doesNotUnderstand: #(#print aPacket) is
executed
– node1 is an instance of Node so doesNotUnderstand: is
looked up in the class Node
– doesNotUnderstand: is not defined in Node => lookup
continues in Object
– doesNotUnderstand: is defined in Object => lookup stops
+ method executed (open a dialog box)
14
S.Ducasse 15
Graphically…
1
2
3
4
5
S.Ducasse 16
Roadmap
• Inheritance
• Method lookup
• Self/super difference
S.Ducasse 17
How to Invoke Overridden
Methods?• Solution: Send messages to super
• When a packet is not addressed to a workstation, we just want to
pass the packet to the next node, i.e., we want to perform the
default behavior defined by Node.
Workstation>>accept: aPacket
(aPacket isAddressedTo: self)
ifTrue:[Transcript show: 'Packet accepted by the Workstation ',
self name asString]
ifFalse: [super accept: aPacket]
• Design Hint: Do not send messages to super with different
selectors than the original one. It introduces implicit dependency
between methods with different names.
S.Ducasse 18
The semantics of super
• Like self, super is a pseudo-variable that refers to the
receiver of the message.
• It is used to invoke overridden methods.
• When using self, the lookup of the method begins in the
class of the receiver.
• When using super, the lookup of the method begins in the
superclass of the class of the method containing the
super expression
S.Ducasse 19
super changes lookup starting class
• A new bar
• -> 10
• B new bar
• -> 10 + 10
• C new bar
• -> 50 + 50
S.Ducasse 20
super is NOT the superclass of the receiver
class
Suppose the WRONG hypothesis: “The semantics of
super is to start the lookup of a method in the
superclass of the receiver class”
S.Ducasse 21
super is NOT the superclass of the receiver
class
mac is instance of ColoredWorkStation
Lookup starts in ColoredWorkStation
Not found so goes up...
accept: is defined in Workstation
lookup stops
method accept: is executed
Workstation>>accept: does a super
send
Our hypothesis: start in the super of the
class of the receiver
=> superclass of class of a ColoredWorkstation
is ... Workstation !
Therefore we look in workstation again!!!
S.Ducasse 22
What you should know
• Inheritance of instance variables is made at class
definition time.
• Inheritance of behavior is dynamic.
• self **always** represents the receiver.
• Method lookup starts in the class of the receiver.
• super represents the receiver but method lookup
starts in the superclass of the class using it.
• Self is dynamic vs. super is static.

More Related Content

PPT
10 - OOP - Inheritance (b)
PPT
7 - OOP - OO Concepts
PDF
Pharo Hands-On: 02 syntax
PDF
Pharo Hands-on: 05 object model
PPT
Stoop sed-class initialization
PPT
Stoop 450-s unit
PPT
12 virtualmachine
PPT
Stoop 390-instruction stream
10 - OOP - Inheritance (b)
7 - OOP - OO Concepts
Pharo Hands-On: 02 syntax
Pharo Hands-on: 05 object model
Stoop sed-class initialization
Stoop 450-s unit
12 virtualmachine
Stoop 390-instruction stream

Viewers also liked (20)

PPT
Stoop sed-smells
PPT
PPT
12 - Conditions and Loops
PPT
Stoop ed-inheritance composition
PPT
Stoop metaclasses
PPT
Debugging VisualWorks
PPT
Double Dispatch
PPT
Stoop 422-naming idioms
PPT
PPT
Stoop ed-class forreuse
PPT
Stoop 436-strategy
PPT
PPT
PPT
Stoop 439-decorator
PPT
Stoop 430-design patternsintro
PPT
Stoop 416-lsp
PPT
Stoop 400 o-metaclassonly
PPT
11 - OOP - Numbers
PPT
10 reflection
PPT
Stoop 414-smalltalk elementsofdesign
Stoop sed-smells
12 - Conditions and Loops
Stoop ed-inheritance composition
Stoop metaclasses
Debugging VisualWorks
Double Dispatch
Stoop 422-naming idioms
Stoop ed-class forreuse
Stoop 436-strategy
Stoop 439-decorator
Stoop 430-design patternsintro
Stoop 416-lsp
Stoop 400 o-metaclassonly
11 - OOP - Numbers
10 reflection
Stoop 414-smalltalk elementsofdesign
Ad

Similar to 10 - OOP - Inheritance (a) (20)

PPT
9 - OOP - Smalltalk Classes (b)
PPT
9 - OOP - Smalltalk Classes (a)
PPT
9 - OOP - Smalltalk Classes (c)
PPT
Stoop 304-metaclasses
PPT
4 - OOP - Taste of Smalltalk (VisualWorks)
PPT
4 - OOP - Taste of Smalltalk (Squeak)
PPT
Stoop 305-reflective programming5
PPT
Stoop 423-smalltalk idioms
KEY
Ruby objects
PPT
5 - OOP - Smalltalk in a Nutshell (b)
PPT
5 - OOP - Smalltalk in a Nutshell (a)
PDF
Objective c runtime
PDF
Metaprogramming in Ruby
PPT
5 - OOP - Smalltalk in a Nutshell (c)
KEY
Parte II Objective C
PPT
8 - OOP - Syntax & Messages
PPT
Stoop 413-abstract classes
PDF
The Ruby Object Model by Rafael Magana
KEY
Ruby Internals
PPT
inheritance
9 - OOP - Smalltalk Classes (b)
9 - OOP - Smalltalk Classes (a)
9 - OOP - Smalltalk Classes (c)
Stoop 304-metaclasses
4 - OOP - Taste of Smalltalk (VisualWorks)
4 - OOP - Taste of Smalltalk (Squeak)
Stoop 305-reflective programming5
Stoop 423-smalltalk idioms
Ruby objects
5 - OOP - Smalltalk in a Nutshell (b)
5 - OOP - Smalltalk in a Nutshell (a)
Objective c runtime
Metaprogramming in Ruby
5 - OOP - Smalltalk in a Nutshell (c)
Parte II Objective C
8 - OOP - Syntax & Messages
Stoop 413-abstract classes
The Ruby Object Model by Rafael Magana
Ruby Internals
inheritance
Ad

More from The World of Smalltalk (19)

PDF
05 seaside canvas
PPT
PPT
09 metaclasses
PPT
08 refactoring
PPT
07 bestpractice
PPT
03 standardclasses
PPT
Stoop sed-sharing ornot
PPT
Stoop sed-class initialization
PPT
Stoop ed-unit ofreuse
PPT
Stoop ed-subtyping subclassing
PPT
Stoop ed-some principles
PPT
PPT
Stoop ed-frameworks
PPT
Stoop ed-dual interface
PPT
Stoop 440-adaptor
05 seaside canvas
09 metaclasses
08 refactoring
07 bestpractice
03 standardclasses
Stoop sed-sharing ornot
Stoop sed-class initialization
Stoop ed-unit ofreuse
Stoop ed-subtyping subclassing
Stoop ed-some principles
Stoop ed-frameworks
Stoop ed-dual interface
Stoop 440-adaptor

Recently uploaded (20)

PPTX
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
PPTX
TNA_Presentation-1-Final(SAVE)) (1).pptx
PDF
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
PDF
Empowerment Technology for Senior High School Guide
PPTX
Share_Module_2_Power_conflict_and_negotiation.pptx
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PPTX
Computer Architecture Input Output Memory.pptx
PDF
AI-driven educational solutions for real-life interventions in the Philippine...
DOCX
Cambridge-Practice-Tests-for-IELTS-12.docx
PPTX
History, Philosophy and sociology of education (1).pptx
PDF
Hazard Identification & Risk Assessment .pdf
PDF
IGGE1 Understanding the Self1234567891011
PPTX
Unit 4 Computer Architecture Multicore Processor.pptx
PDF
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PDF
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
PDF
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
TNA_Presentation-1-Final(SAVE)) (1).pptx
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
Empowerment Technology for Senior High School Guide
Share_Module_2_Power_conflict_and_negotiation.pptx
202450812 BayCHI UCSC-SV 20250812 v17.pptx
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Paper A Mock Exam 9_ Attempt review.pdf.
Computer Architecture Input Output Memory.pptx
AI-driven educational solutions for real-life interventions in the Philippine...
Cambridge-Practice-Tests-for-IELTS-12.docx
History, Philosophy and sociology of education (1).pptx
Hazard Identification & Risk Assessment .pdf
IGGE1 Understanding the Self1234567891011
Unit 4 Computer Architecture Multicore Processor.pptx
Vision Prelims GS PYQ Analysis 2011-2022 www.upscpdf.com.pdf
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
LDMMIA Reiki Yoga Finals Review Spring Summer
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
Τίμαιος είναι φιλοσοφικός διάλογος του Πλάτωνα

10 - OOP - Inheritance (a)

  • 1. S.Ducasse 1 QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture. Stéphane Ducasse Stephane.Ducasse@univ-savoie.fr http://www.iam.unibe.ch/~ducasse/ Inheritance Semantics and Method Lookup
  • 2. S.Ducasse 2 Goal • Inheritance • Method lookup • Self/super difference
  • 3. S.Ducasse 3 Inheritance • Do not want to rewrite everything! • Often we want small changes • We would like to reuse and extend existing behavior • Solution: class inheritance • Each class defines or refines the definition • of its ancestors
  • 4. S.Ducasse 4 Inheritance • New classes • Can add state and behavior: • color, borderColor, borderWidth, • totalArea • Can specialize ancestor behavior • intersect: • Can use ancestor’s behavior and state • Can redefine ancestor’s behavior • area to return totalArea
  • 5. S.Ducasse 5 Inheritance in Smalltalk • Single inheritance • Static for the instance variables • At class creation time the instance variables are collected from the superclasses and the class. No repetition of instance variables. • Dynamic for the methods • Late binding (all virtual) methods are looked up at run-time depending on the dynamic type of the receiver.
  • 6. S.Ducasse 6 Message Sending • receiver selector args • Sending a message = looking up the method that should be executed and executing it • Looking up a method: When a message (receiver selector args) is sent, the method corresponding to the message selector is looked up through the inheritance chain.
  • 7. S.Ducasse 7 Method Lookup • Two steps process • The lookup starts in the CLASS of the RECEIVER. • If the method is defined in the method dictionary, it is returned. • Otherwise the search continues in the superclasses of the receiver's class. If no method is found and there is no superclass to explore (class Object), this is an ERROR
  • 8. S.Ducasse 8 Lookup: class and inheritance
  • 10. S.Ducasse 10 Method Lookup starts in Receiver Class aB foo (1) aB class => B (2) Is foo defined in B? (3) Foo is executed -> 50 aB bar (1) aB class => B (2) Is bar defined in B? (3) Is bar defined in A? (4) bar executed (5) Self class => B (6) Is foo defined in B (7) Foo is executed -> 50
  • 11. S.Ducasse 11 self **always** represents the receiver • A new foo • -> 10 • B new foo • -> 10 • C new foo • -> 50 • A new bar • -> 10 • B new bar • -> 10 • C new bar • -> 50
  • 12. S.Ducasse 12 When message is not found • If no method is found and there is no superclass to explore (class Object), a new method called #doesNotUnderstand: is sent to the receiver, with a representation of the initial message.
  • 14. S.Ducasse 14 …in Smalltalk • node1 print: aPacket – node is an instance of Node – print: is looked up in the class Node – print: is not defined in Node > lookup continues in Object – print: is not defined in Object => lookup stops + exception – message: node1 doesNotUnderstand: #(#print aPacket) is executed – node1 is an instance of Node so doesNotUnderstand: is looked up in the class Node – doesNotUnderstand: is not defined in Node => lookup continues in Object – doesNotUnderstand: is defined in Object => lookup stops + method executed (open a dialog box) 14
  • 16. S.Ducasse 16 Roadmap • Inheritance • Method lookup • Self/super difference
  • 17. S.Ducasse 17 How to Invoke Overridden Methods?• Solution: Send messages to super • When a packet is not addressed to a workstation, we just want to pass the packet to the next node, i.e., we want to perform the default behavior defined by Node. Workstation>>accept: aPacket (aPacket isAddressedTo: self) ifTrue:[Transcript show: 'Packet accepted by the Workstation ', self name asString] ifFalse: [super accept: aPacket] • Design Hint: Do not send messages to super with different selectors than the original one. It introduces implicit dependency between methods with different names.
  • 18. S.Ducasse 18 The semantics of super • Like self, super is a pseudo-variable that refers to the receiver of the message. • It is used to invoke overridden methods. • When using self, the lookup of the method begins in the class of the receiver. • When using super, the lookup of the method begins in the superclass of the class of the method containing the super expression
  • 19. S.Ducasse 19 super changes lookup starting class • A new bar • -> 10 • B new bar • -> 10 + 10 • C new bar • -> 50 + 50
  • 20. S.Ducasse 20 super is NOT the superclass of the receiver class Suppose the WRONG hypothesis: “The semantics of super is to start the lookup of a method in the superclass of the receiver class”
  • 21. S.Ducasse 21 super is NOT the superclass of the receiver class mac is instance of ColoredWorkStation Lookup starts in ColoredWorkStation Not found so goes up... accept: is defined in Workstation lookup stops method accept: is executed Workstation>>accept: does a super send Our hypothesis: start in the super of the class of the receiver => superclass of class of a ColoredWorkstation is ... Workstation ! Therefore we look in workstation again!!!
  • 22. S.Ducasse 22 What you should know • Inheritance of instance variables is made at class definition time. • Inheritance of behavior is dynamic. • self **always** represents the receiver. • Method lookup starts in the class of the receiver. • super represents the receiver but method lookup starts in the superclass of the class using it. • Self is dynamic vs. super is static.