![Page 1: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/1.jpg)
Lezione 14: Multiple inheritance
Fondamenti della Programmazione:Metodi Evoluti
Prof. Enrico Nardelli
![Page 2: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/2.jpg)
Combining abstractions
Given the classes
TRAIN_CAR, RESTAURANT
how would you implement a DINER?
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 2
![Page 3: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/3.jpg)
Examples of multiple inheritance
Combining separate abstractions:
Restaurant, train car Calculator, watch Home, vehicle Taxi, bus
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 3
![Page 4: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/4.jpg)
WarningForget all you have heard!
Multiple inheritance is not the works of the devilMultiple inheritance is not bad for your teeth
(Even though Microsoft Word apparently does not like it:
)
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 4
![Page 5: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/5.jpg)
An example of repeated inheritance
A class with two or more parents sharing a same grand-parent.
Examples that come to mind: ASSISTANT inherits from TEACHER and STUDENT.
TEACHER STUDENT
ASSISTANT
UNIVERSITY_MEMBER id
This is a case of repeated inheritance
????
????
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 5
![Page 6: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/6.jpg)
repeated and multiple inheritance
A
D
B C
A
D
Multiple inheritance from B and CRepeated inheritance from A(In Eiffel is found often; why?)
Cannot happen in Eiffel
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 6
![Page 7: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/7.jpg)
Indirect repeated inheritance
In Eiffel, repeated inheritance happens often:
CB
D
ANY
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 7
![Page 8: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/8.jpg)
Another warning
The language part of this lecture are Eiffel-oriented
Java and C# mechanisms (single inheritance from classes, multiple inheritance from interfaces) will also be discussed
C++ also has multiple inheritance, but it will not be described
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 8
![Page 9: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/9.jpg)
Composite figures
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 9
![Page 10: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/10.jpg)
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 10
![Page 11: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/11.jpg)
Multiple inheritance: Composite figures
A composite figure
Simple figures
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 11
![Page 12: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/12.jpg)
Defining the notion of composite figure
COMPOSITE_FIGURE inherits different features frommore than one parent: this is multiple inheritance
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 12
COMPOSITE_FIGURE
centerdisplayhiderotatemove…
countputremove…
FIGURELIST
[FIGURE ]
![Page 13: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/13.jpg)
In the overall structure
COMPOSITE_FIGURE
FIGURELIST
[FIGURE ]
OPEN_FIGURE
CLOSED_FIGURE
SEGMENT POLYLINE POLYGON ELLIPSE
RECTANGLE
SQUARE
CIRCLE
TRIANGLE
perimeter+
perimeter*
perimeter++
diagonal
perimeter++
perimeter++
perimeter+
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 13
display*move*rotate*
![Page 14: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/14.jpg)
figs: LIST [FIGURE]…
fromfigs.start until figs.after loop
figs.item.display
figs.forth
end
Working with polymorphic data structures
Dynamic binding
(POLYGON) (CIRCLE) (POLYGON)(CIRCLE) (ELLIPSE)14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 14
(from 10-EREDITARIETA’)
![Page 15: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/15.jpg)
figs: LIST [FIGURE]…
across figs as c loop
c item display
end
Working with polymorphic data structures
Dynamic binding
(POLYGON) (CIRCLE) (POLYGON)(CIRCLE) (ELLIPSE)14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 15
(from 10-EREDITARIETA’)
![Page 16: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/16.jpg)
Definition (Polymorphism, adapted)
An attachment (assignment or argument passing) is polymorphic if its target entity and source expression have different types.
An entity or expression is polymorphic if – as a result of polymorphic attachments – it may at runtime become attached to objects of different types.
A container data structure is polymorphic if it may contain references to objects of different types.
Polymorphism is the existence of these possibilities.
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 16
(from 10-EREDITARIETA’)
![Page 17: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/17.jpg)
A composite figure as a list
Cursor
item
forth
after
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 17
![Page 18: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/18.jpg)
Requires dynamic binding
class COMPOSITE_FIGURE inheritFIGURELIST [FIGURE]
featuredisplay
-- Display each constituent figure in turn.do
from start until after loopitem.display
forthend
end... Similarly for move, rotate etc. ...
end
Composite figures
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 18
![Page 19: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/19.jpg)
Multiple inheritance: Combining abstractions
COMPARABLE NUMERIC
STRING COMPLEX
INTEGER
REAL
<, <=,>, >=, …
+, –, *, / …
(total order relation)
(commutative ring)
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 19
![Page 20: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/20.jpg)
How do we write COMPARABLE?
deferred class COMPARABLE [G ] feature
end
less alias "<" (x : COMPARABLE [G ]): BOOLEANdeferredend
less_equal alias "<=" (x : COMPARABLE [G ]): BOOLEANdo
Result := (Current < x or (Current = x))end
greater alias ">" (x : COMPARABLE [G ]): BOOLEANdo Result := (x < Current) end
greater_equal alias ">=" (x : COMPARABLE [G ]): BOOLEANdo Result := (x <= Current) end
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 20
![Page 21: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/21.jpg)
Java and .NET and C# solution
Single inheritance only for classes
Multiple inheritance from interfaces
An interface is like a fully deferred class, with no implementations (do clauses), no attributes (and also no contracts): it’s only specification
A class may inherit from: At most one class Any number of interfaces
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 21
![Page 22: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/22.jpg)
Deferred classes vs Java interfaces (1)
• Java interfaces are “entirely deferred”• Only method (routine) definitions• No method implementations• No attributes• No contracts
• Eiffel deferred classes can include effective features, possibly relying on deferred ones, as in the COMPARABLE example
• Flexible mechanism to implement abstractions progressively
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 22
![Page 23: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/23.jpg)
Deferred classes vs Java interfaces (2)
Java requires that every descendant of an interface must provide implementations of all interface’s features.
To be able to flexibly model reality we need the full spectrum from fully abstract (i.e., fully deferred) to fully implemented classes provided by Eiffel
Multiple inheritance is here to help us combine abstractions
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 23
![Page 24: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/24.jpg)
Which ?
ff A B
f
Resolving name clashes
rename f as f_A
C f_A,
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 28
class C inheritA rename f as f_A endB
…
![Page 25: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/25.jpg)
Consequences of renaming (1)
a1 : Ab1 : Bc1 : C...
c1.fc1.f_A
a1.fa1.f_A
b1.fb1.f_A
rename f as f_A
C
f A B
f_A, f
f
OK
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 29
Invalid!
OK
OK
OK
Invalid!
Version from A
Version from B
Version from A
Version from B
In class C
![Page 26: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/26.jpg)
Renaming and redefinitionRenaming keeps the feature behavior and changes its name
Redefinition changes the feature behavior and keeps its name
It is possible to combine both:
class B inherit
Arename f as f_Aredefine f_Aend
…
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 31
![Page 27: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/27.jpg)
An application of renaming
Provide locally better adapted terminology.Example: child (TREE ); subwindow (WINDOW)
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 32
![Page 28: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/28.jpg)
‘‘Graphical’’ features: height, width, change_height, change_width, xpos, ypos, move...
‘‘Hierarchical’’ features: superwindow, subwindows, change_subwindow, add_subwindow...
class WINDOW inheritRECTANGLETREE [WINDOW]
renameparent as superwindow,children as subwindows,add_child as add_subwindow…
endfeature
...end
Renaming to improve feature terminology
BUT: see style rules about uniformity of
feature names
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 33
![Page 29: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/29.jpg)
Are all name clashes bad?
A name clash must be removed unless it is: Under repeated inheritance (i.e. not a real clash), OR All inherited features with the same name are such that
• They all have compatible signatures• At most one of them is effective
Semantics of the latter case: All features are merged into a single one If there is an effective feature, its implementation is the one
which is used
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 34
![Page 30: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/30.jpg)
Feature merging
A B C
D
f +f * f *
* Deferred+ Effective
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 35
![Page 31: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/31.jpg)
Feature merging: case of effective features
A B C
D
f +f + f +
* Deferred+ Effective-- Undefine
f --f --
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 36
deferred classD
inheritA
undefine f end
Bundefine f end
C
feature...
end
![Page 32: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/32.jpg)
Feature merging: case of different names (1)
A B C
D
g * f *
* Deferred+ Effective
Renaming
g f h f
classD
inheritA
renameg as f
end
B
Crename
h as fend
feature...
end14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 37
Desired name
h +
![Page 33: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/33.jpg)
Feature merging: case of different names (2)
A B C
D
f + g +
f --
f --
classD
inheritA
undefine f end
Brename
g as fundefine fend
Crename
h as fend
feature ... end
h f
g f
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 38
Desired name
h +
![Page 34: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/34.jpg)
Feature call after merging
a1: A b1: B c1: C d1: Da1.g b1.f c1.h d1.f
d1.gd1.h
A B C
D
g +
f +
h +
g f h ff
--f
--
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 39
Invalid!
OKOKOKOK
Invalid!
In class D
![Page 35: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/35.jpg)
Sharing and replication
Features such as f, not renamed along any of the inheritance paths, will be shared.Features such as g, inherited under different names, will be replicated: there are two names to execute the same action
A
B C
D
fg
g g_b g g_c
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 40
![Page 36: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/36.jpg)
The need for select
A potential ambiguity arises because of polymorphism and dynamic binding:a1 : ANY; t1 : LIST; d1 : D…a1.copy (…)t1.copy (…)d1.copy (…)a1 := t1a1.copy (…)
t1 := d1t1.copy (…)
a1 := d1a1.copy (…)
copy ++
is_equal ++
copy copy_Cis_equal is_equal_C
CLIST
D
copy
is_equalANY
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 41
this renaming is mandatory to avoid name clash
ANY version
LIST or C version ??
LIST version
LIST version
LIST version
LIST version
The compiler cannot decide !
![Page 37: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/37.jpg)
When the need arises?• This happens whenever, through the combination of
renaming (and possibly redefinition) in differentinheritance paths, in a class X there is more than oneversion of an inherited feature f (repeatedly inheritedfeature)
• These versions will have different names (due to renaming) and might have different behaviours (due to redefinition)
• If a variable of the ancestor class which has provided the original version of the feature get assigned a variable of class X neither the compiler nor the runtime can decide which version of feature f should be used
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 42
![Page 38: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/38.jpg)
classD
inheritLIST [T ]
selectcopy, is_equal
end
Crename
copy as copy_C, is_equal as is_equal_C,
...end
Removing the ambiguity
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 43
The version from LIST is used under dynamic
binding in the case of a polymorphic target with a
possible ambiguity
![Page 39: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/39.jpg)
Order for redeclaration clauses (standard specif.)class
AN_HEIR
inheritA_PARENT
undefinefeature_A, feature_B, …
redefinefeature_C, feature_D, …
renamefeature_C, feature_D, …
export{class_X, class_Y, … } feature_A, feature_B, …{class_W, class_Z, … } feature_C, feature_D, …
selectfeature_C, feature_D, …
end
end
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 44
change implementation
give a new name
change the visibility status
selection for dynamic binding
make deferred
Prescribed in ECMA, not yet implemented!
![Page 40: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/40.jpg)
Order for redeclaration clauses (actual)class
AN_HEIR
inheritA_PARENT
renamefeature_C, feature_D, …
export{class_X, class_Y, … } feature_A, feature_B, …{class_W, class_Z, … } feature_C, feature_D, …
undefinefeature_A, feature_B, …
redefinefeature_C, feature_D, …
selectfeature_C, feature_D, …
end
end
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 45
change implementation
give a new name
change the visibility status
selection for dynamic binding
make deferred
The one actually implemented in Eiffel
![Page 41: Fondamenti della Programmazione: Metodi Evoluti Prof. Enrico …nardelli/fondamenti-programmazione-… · Java and .NET and C# solution. Single inheritance only for classes. Multiple](https://reader036.vdocuments.site/reader036/viewer/2022062916/5ec7506e3e6d9e2f275e5672/html5/thumbnails/41.jpg)
What we have seen
A number of games one can play with inheritance: Multiple inheritance Feature merging Repeated inheritance
14-EREDITARIETA'-MULTIPLA Rev. 2.1.1 (2018-19) by Enrico Nardelli (based on touch.ethz.ch) 46