SlideShare ist ein Scribd-Unternehmen logo
1 von 23
S.Ducasse 1
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
Stéphane Ducasse
Stephane.Ducasse@univ-savoie.fr
http://www.listic.univ-savoie.fr/~ducasse/
Inheritance Semantics
and Method Lookup
S.Ducasse 2
License: CC-Attribution-ShareAlike 2.0
http://creativecommons.org/licenses/by-sa/2.0/
S.Ducasse 3
Goal
• Inheritance
• Method lookup
• Self/super difference
S.Ducasse 4
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 5
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 6
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 7
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 8
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 9
Lookup: class and inheritance
S.Ducasse 10
Some Cases
S.Ducasse 11
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 12
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 13
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 14
Graphically…
1
2
S.Ducasse 15
…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)
15
S.Ducasse 16
Graphically…
1
2
3
4
5
S.Ducasse 17
Roadmap
• Inheritance
• Method lookup
• Self/super difference
S.Ducasse 18
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 19
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 20
super changes lookup starting class
• A new bar
• -> 10
• B new bar
• -> 10 + 10
• C new bar
• -> 50 + 50
S.Ducasse 21
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 22
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 23
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.

Weitere ähnliche Inhalte

Andere mochten auch (20)

Stoop 400-metaclass only
Stoop 400-metaclass onlyStoop 400-metaclass only
Stoop 400-metaclass only
 
Stoop 421-design heuristics
Stoop 421-design heuristicsStoop 421-design heuristics
Stoop 421-design heuristics
 
9 - OOP - Smalltalk Classes (b)
9 - OOP - Smalltalk Classes (b)9 - OOP - Smalltalk Classes (b)
9 - OOP - Smalltalk Classes (b)
 
08 refactoring
08 refactoring08 refactoring
08 refactoring
 
14 - Exceptions
14 - Exceptions14 - Exceptions
14 - Exceptions
 
Stoop 450-s unit
Stoop 450-s unitStoop 450-s unit
Stoop 450-s unit
 
01 intro
01 intro01 intro
01 intro
 
Stoop ed-subtyping subclassing
Stoop ed-subtyping subclassingStoop ed-subtyping subclassing
Stoop ed-subtyping subclassing
 
Stoop 431-visitor
Stoop 431-visitorStoop 431-visitor
Stoop 431-visitor
 
Stoop 300-block optimizationinvw
Stoop 300-block optimizationinvwStoop 300-block optimizationinvw
Stoop 300-block optimizationinvw
 
Stoop 437-proxy
Stoop 437-proxyStoop 437-proxy
Stoop 437-proxy
 
Stoop 434-composite
Stoop 434-compositeStoop 434-composite
Stoop 434-composite
 
Stoop 436-strategy
Stoop 436-strategyStoop 436-strategy
Stoop 436-strategy
 
Stoop ed-lod
Stoop ed-lodStoop ed-lod
Stoop ed-lod
 
Stoop 423-smalltalk idioms
Stoop 423-smalltalk idiomsStoop 423-smalltalk idioms
Stoop 423-smalltalk idioms
 
Stoop ed-frameworks
Stoop ed-frameworksStoop ed-frameworks
Stoop ed-frameworks
 
Stoop 414-smalltalk elementsofdesign
Stoop 414-smalltalk elementsofdesignStoop 414-smalltalk elementsofdesign
Stoop 414-smalltalk elementsofdesign
 
11 - OOP - Numbers
11 - OOP - Numbers11 - OOP - Numbers
11 - OOP - Numbers
 
Stoop ed-unit ofreuse
Stoop ed-unit ofreuseStoop ed-unit ofreuse
Stoop ed-unit ofreuse
 
12 - Conditions and Loops
12 - Conditions and Loops12 - Conditions and Loops
12 - Conditions and Loops
 

Ähnlich wie 10 - OOP - Inheritance (b)

4 - OOP - Taste of Smalltalk (Tamagoshi)
4 - OOP - Taste of Smalltalk (Tamagoshi)4 - OOP - Taste of Smalltalk (Tamagoshi)
4 - OOP - Taste of Smalltalk (Tamagoshi)The World of Smalltalk
 
Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1PRN USM
 
Uniform and Safe Metaclass Composition
Uniform and Safe Metaclass CompositionUniform and Safe Metaclass Composition
Uniform and Safe Metaclass CompositionESUG
 
Classboxes, nested methods, and real private methods
Classboxes, nested methods, and real private methodsClassboxes, nested methods, and real private methods
Classboxes, nested methods, and real private methodsShugo Maeda
 
ITT 2015 - Ash Furrow - Lessons from Production Swift
ITT 2015 - Ash Furrow - Lessons from Production SwiftITT 2015 - Ash Furrow - Lessons from Production Swift
ITT 2015 - Ash Furrow - Lessons from Production SwiftIstanbul Tech Talks
 
Ruby :: Training 1
Ruby :: Training 1Ruby :: Training 1
Ruby :: Training 1Pavel Tyk
 
06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.pptParikhitGhosh1
 
4 - OOP - Taste of Smalltalk (VisualWorks)
4 - OOP - Taste of Smalltalk (VisualWorks)4 - OOP - Taste of Smalltalk (VisualWorks)
4 - OOP - Taste of Smalltalk (VisualWorks)The World of Smalltalk
 

Ähnlich wie 10 - OOP - Inheritance (b) (20)

4 - OOP - Taste of Smalltalk (Tamagoshi)
4 - OOP - Taste of Smalltalk (Tamagoshi)4 - OOP - Taste of Smalltalk (Tamagoshi)
4 - OOP - Taste of Smalltalk (Tamagoshi)
 
Debugging VisualWorks
Debugging VisualWorksDebugging VisualWorks
Debugging VisualWorks
 
Stoop 304-metaclasses
Stoop 304-metaclassesStoop 304-metaclasses
Stoop 304-metaclasses
 
9 - OOP - Smalltalk Classes (a)
9 - OOP - Smalltalk Classes (a)9 - OOP - Smalltalk Classes (a)
9 - OOP - Smalltalk Classes (a)
 
8 - OOP - Syntax & Messages
8 - OOP - Syntax & Messages8 - OOP - Syntax & Messages
8 - OOP - Syntax & Messages
 
6 Object Oriented Programming
6 Object Oriented Programming6 Object Oriented Programming
6 Object Oriented Programming
 
8 - OOP - Smalltalk Syntax
8 - OOP - Smalltalk Syntax8 - OOP - Smalltalk Syntax
8 - OOP - Smalltalk Syntax
 
5 - OOP - Smalltalk in a Nutshell (b)
5 - OOP - Smalltalk in a Nutshell (b)5 - OOP - Smalltalk in a Nutshell (b)
5 - OOP - Smalltalk in a Nutshell (b)
 
Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1
 
Uniform and Safe Metaclass Composition
Uniform and Safe Metaclass CompositionUniform and Safe Metaclass Composition
Uniform and Safe Metaclass Composition
 
Classboxes, nested methods, and real private methods
Classboxes, nested methods, and real private methodsClassboxes, nested methods, and real private methods
Classboxes, nested methods, and real private methods
 
ITT 2015 - Ash Furrow - Lessons from Production Swift
ITT 2015 - Ash Furrow - Lessons from Production SwiftITT 2015 - Ash Furrow - Lessons from Production Swift
ITT 2015 - Ash Furrow - Lessons from Production Swift
 
Ruby :: Training 1
Ruby :: Training 1Ruby :: Training 1
Ruby :: Training 1
 
06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt
 
1 - OOP
1 - OOP1 - OOP
1 - OOP
 
Stoop 303-advanced blocks
Stoop 303-advanced blocksStoop 303-advanced blocks
Stoop 303-advanced blocks
 
INHERITANCE.pptx
INHERITANCE.pptxINHERITANCE.pptx
INHERITANCE.pptx
 
4 - OOP - Taste of Smalltalk (VisualWorks)
4 - OOP - Taste of Smalltalk (VisualWorks)4 - OOP - Taste of Smalltalk (VisualWorks)
4 - OOP - Taste of Smalltalk (VisualWorks)
 
Ruby object model
Ruby object modelRuby object model
Ruby object model
 
9 - OOP - Smalltalk Classes (c)
9 - OOP - Smalltalk Classes (c)9 - OOP - Smalltalk Classes (c)
9 - OOP - Smalltalk Classes (c)
 

Mehr von The World of Smalltalk (20)

05 seaside canvas
05 seaside canvas05 seaside canvas
05 seaside canvas
 
99 questions
99 questions99 questions
99 questions
 
13 traits
13 traits13 traits
13 traits
 
12 virtualmachine
12 virtualmachine12 virtualmachine
12 virtualmachine
 
11 bytecode
11 bytecode11 bytecode
11 bytecode
 
10 reflection
10 reflection10 reflection
10 reflection
 
09 metaclasses
09 metaclasses09 metaclasses
09 metaclasses
 
07 bestpractice
07 bestpractice07 bestpractice
07 bestpractice
 
06 debugging
06 debugging06 debugging
06 debugging
 
05 seaside
05 seaside05 seaside
05 seaside
 
04 idioms
04 idioms04 idioms
04 idioms
 
03 standardclasses
03 standardclasses03 standardclasses
03 standardclasses
 
02 basics
02 basics02 basics
02 basics
 
Stoop sed-smells
Stoop sed-smellsStoop sed-smells
Stoop sed-smells
 
Stoop sed-sharing ornot
Stoop sed-sharing ornotStoop sed-sharing ornot
Stoop sed-sharing ornot
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
Stoop metaclasses
Stoop metaclassesStoop metaclasses
Stoop metaclasses
 
Stoop ed-some principles
Stoop ed-some principlesStoop ed-some principles
Stoop ed-some principles
 
Stoop ed-inheritance composition
Stoop ed-inheritance compositionStoop ed-inheritance composition
Stoop ed-inheritance composition
 
Stoop ed-dual interface
Stoop ed-dual interfaceStoop ed-dual interface
Stoop ed-dual interface
 

Kürzlich hochgeladen

The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 

Kürzlich hochgeladen (20)

The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 

10 - OOP - Inheritance (b)

  • 1. S.Ducasse 1 QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture. Stéphane Ducasse Stephane.Ducasse@univ-savoie.fr http://www.listic.univ-savoie.fr/~ducasse/ Inheritance Semantics and Method Lookup
  • 2. S.Ducasse 2 License: CC-Attribution-ShareAlike 2.0 http://creativecommons.org/licenses/by-sa/2.0/
  • 3. S.Ducasse 3 Goal • Inheritance • Method lookup • Self/super difference
  • 4. S.Ducasse 4 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
  • 5. S.Ducasse 5 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
  • 6. S.Ducasse 6 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.
  • 7. S.Ducasse 7 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.
  • 8. S.Ducasse 8 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
  • 9. S.Ducasse 9 Lookup: class and inheritance
  • 11. S.Ducasse 11 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
  • 12. S.Ducasse 12 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
  • 13. S.Ducasse 13 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.
  • 15. S.Ducasse 15 …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) 15
  • 17. S.Ducasse 17 Roadmap • Inheritance • Method lookup • Self/super difference
  • 18. S.Ducasse 18 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.
  • 19. S.Ducasse 19 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
  • 20. S.Ducasse 20 super changes lookup starting class • A new bar • -> 10 • B new bar • -> 10 + 10 • C new bar • -> 50 + 50
  • 21. S.Ducasse 21 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”
  • 22. S.Ducasse 22 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!!!
  • 23. S.Ducasse 23 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.