Download presentation
Presentation is loading. Please wait.
Published byColleen Gray Modified over 9 years ago
1
1 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10
2
2 Today Multiple inheritance
3
3 Combining abstractions Given the classes TRAIN_CAR, RESTAURANT how would you implement a DINER?
4
4 Inheritance is never the only way Given the classes TRAIN_CAR, RESTAURANT how would you implement a DINER? You could have an attribute in TRAIN_CAR train_service: SERVICE Then have RESTAURANT inherit from SERVICE This is flexible if the kind of service may change to a type that is unrelated to TRAIN_CAR Changes in TRAIN_CAR do not affect SERVICE easily
5
5 Examples of multiple inheritance Combining separate abstractions: Restaurant, train car Calculator, watch Other examples? Hands-On
6
6 Examples of multiple inheritance Combining separate abstractions: Restaurant, train car Calculator, watch Other examples? Teacher, student Home, vehicle Hands-On
7
7 Multiple inheritance: Combining abstractions COMPARABLE NUMERIC STRING COMPLEX INTEGER REAL, >=, … +, –, , / … (total order relation) (commutative ring)
8
8 Composite figures
9
9 Multiple inheritance: Composite figures A composite figure Simple figures
10
10 Defining the notion of composite figure COMPOSITE_ FIGURE center display hide rotate move … count put remove … FIGURE V_LIST [FIGURE ]
11
11 In the overall structure COMPOSITE_ FIGURE FIGURE V_LIST [FIGURE ] OPEN_ FIGURE CLOSED_ FIGURE SEGMENTPOLYLINEPOLYGON ELLIPSE RECTANGLE SQUARE CIRCLE TRIANGLE perimeter + perimeter* perimeter ++ diagonal perimeter ++ perimeter +
12
12 A composite figure as a list Cursor item forth after before
13
13 Composite figures class COMPOSITE_FIGURE inherit FIGURE V_LIST [FIGURE] feature display -- Display each constituent figure in turn. do from start until after loop item. display forth end end... Similarly for move, rotate etc.... end Requires dynamic binding
14
14 An alternative solution: the composite pattern COMPOSITE_ FIGURE FIGURE LIST [FIGURE ] OPEN_ FIGURE CLOSED_ FIGURE SEGMENTPOLYLINEPOLYGON ELLIPSE RECTANGLE SQUARE CIRCLE TRIANGLE perimeter + perimeter* perimeter ++ diagonal perimeter ++ perimeter + figure_list
15
15 The Java-C# solution No multiple inheritance for classes “Interfaces”: specification only (but no contracts) Similar to completely deferred classes (with no effective feature) A class may inherit from: At most one class Any number of interfaces
16
16 Lessons from this example Typical example of program with holes We need the full spectrum from fully abstract (fully deferred) to fully implemented classes Multiple inheritance is there to help us combine abstractions
17
17 Multiple inheritance: Name clashes f C f A B ? Hands-On
18
18 Resolving name clashes f rename f as A_f C f A B A_f, f Hands-On
19
19 Consequences of renaming Valid or invalid? a1 : A b1 : B c1 : C... c1. f a1.A_f c1.A_f b1.f b1.A_f rename f as A_f C f A B A_f, f f Hands-On Invalid Valid Invalid
20
20 Are all name clashes bad? A name clash must be removed unless it is: Under repeated inheritance (i.e. not a real clash) Between features of which at most one is effective (i.e. others are deferred) indirect repeated inheritance direct repeated inheritance
21
21 Feature merging ABC D f + f *f * f *f * Deferred + Effective
22
22 Feature merging: with different names ABC D h + g *g * f *f * Deferred + Effective Renaming g f h f class D inherit A rename g as f end B C rename h as f end feature... end
23
23 Feature merging: effective features ABC D f + Deferred + Effective -- Undefine f --
24
24 Undefinition deferred class T inherit S undefine v end feature... end
25
25 Merging through undefinition class D inherit A undefine f end B C undefine f end feature... end ABC D f + f -- Deferred + Effective -- Undefine
26
26 Merging effective features with different names A B C D h + f + g + f -- class D inherit A undefine f end B rename g as f undefine f end C rename h as f end feature... end h f g f
27
27 Acceptable name clashes If inherited features have all the same names, there is no harmful name clash if: They all have compatible signatures At most one of them is effective Semantics of such a case: Merge all features into one If there is an effective feature, it imposes its implementation
28
28 Feature merging: effective features a1: Ab1: Bc1: Cd1: D a1.gb1.fc1.hd1.f ABC D g+g+ f+f+h+h+ g f h f f-f-f-f-
29
29 Exercise: All-in-one-device PRINTER ALL_IN_ONE_DEVICE Hands-On SCANNER FAX
30
30 Exercise: All-in-one-device class PRINTER feature print_page -- Print a page. do print ("Printer prints a page...") end switch_on -- Switch from ‘off‘ to ‘on‘ do print ("Printer switched on...") end class FAX feature send -- Send a page over the phone net. do print (“Fax sends a page...") end start -- Switch from ‘off‘ to ‘on‘ do print (“Fax switched on...") end Hands-On class SCANNER feature scan_page -- Scan a page. do print (“Scanner scans a page...") end switch_on -- Switch from ‘off‘ to ‘on‘ do print (“Scanner switched on...") end send -- Send data to PC. do print (“Scanner sends data...") end
31
31 Exercise: All-in-one-device PRINTER ALL_IN_ONE_DEVICE Hands-On SCANNER FAX class ALL_IN_ONE_DEVICE inherit... end How to resolve the name clashes? switch_on send
32
32 Exercise: All-in-one-device class ALL_IN_ONE_DEVICE inherit PRINTER rename switch_on as start undefine start end SCANNER rename switch_on as start, send as send_data end FAX rename send as send_message undefine start end feature... end Hands-On
33
33 Valid or invalid? s: SCANNER f: FAX a: ALL_IN_ONE_DEVICE a. switch_on a. print_page f. send_message s. switch_on f. send a. send Hands-On class ALL_IN_ONE_DEVICE inherit PRINTER rename switch_on as start undefine start end SCANNER rename switch_on as start, send as send_data end FAX rename send as send_message undefine start end feature... end Invalid Valid Invalid Valid Invalid
34
34 A special case of multiple inheritance TEACHER STUDENT ASSISTANT UNIVERSITY _MEMBER id This is a case of repeated inheritance ?? ????
35
35 Indirect and direct repeated inheritance A D B C A D
36
36 Multiple is also repeated inheritance A typical case: copy ++ is_equal ++ copy is_equal ?? copy C_copy is_equal C_is_equal C LIST D ANY
37
37 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. A B C D fgfg g g_b g g_c
38
38 The need for select A potential ambiguity arises because of polymorphism and dynamic binding: a1 : ANY d1 : D … a1 := d1 a1. copy (…) copy ++ is_equal ++ copy C_copy is_equal C_is_equal C LIST D copy is_equal ANY
39
39 Removing the ambiguity class D inherit V_LIST [T ] select copy, is_equal end C rename copy as C_copy, is_equal as C_is_equal,... end
40
40 When is a name clash acceptable? (Between n features of a class, all with the same name, immediate or inherited.) They must all have compatible signatures. If more than one is effective, they must all come from a common ancestor feature under repeated inheritance.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.