1 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.

Slides:



Advertisements
Similar presentations
Distributed DBMS©M. T. Özsu & P. Valduriez Ch.15/1 Outline Introduction Background Distributed Database Design Database Integration Semantic Data Control.
Advertisements

© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
1 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 13.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Exercise Session 1: Eiffel Introduction.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 7: References and Assignment.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session November 2008.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 6.
Ceg860 (Prasad)L13MI1 Multiple Inheritance DAG Class Structure : Directed Acyclic Graph.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Object-Oriented Programming (OOP) Lecture No. 6
Chair of Software Engineering OOSC Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering OOSC - Summer Semester Bertrand Meyer Object-Oriented Software Construction Lecture 8: More on inheritance.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 17: Topological Sort Algorithm.
Principles of Object-Oriented Software Development The language Eiffel.
Chair of Software Engineering ATOT - Lecture 8, 28 April Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering OOSC Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 9.
Chair of Software Engineering ATOT - Lecture 9, 30 April Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 9: Abstraction.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 18: Undo/Redo.
Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 9: Contracts and Inheritance (based on work with.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
Chair of Software Engineering ATOT - Lecture 14, 19 May Advanced Topics in Object Technology Bertrand Meyer.
1 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 13.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
Chair of Software Engineering OOSC - Summer Semester Bertrand Meyer Object-Oriented Software Construction Lecture 7: Inheritance.
Data Abstraction: The Walls
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 7.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 9.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 20: Multiple inheritance.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 11.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
Eiffel: Analysis, Design and Programming Bertrand Meyer (Nadia Polikarpova) Chair of Software Engineering.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 9: Abstraction.
Inheritance and Polymorphism CS351 – Programming Paradigms.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
Patterns. PATTERNS A pattern constitutes a set of numbers or objects in which all the members are related with each other by a specific rule. It is also.
1 Dept. of Computer Science & Engineering, York University, Toronto Software Development CSE3311 Composite Pattern.
1 Software Architecture Bertrand Meyer ETH Zurich, March-July 2009 Lecture 11: More patterns: Bridge, Composite, Decorator, Façade, Flyweight.
程建群 博士(Dr. Jason Cheng) 年03月
CS  C++ allows multiple implementation inheritance  Handy for multiple “is-a” situations  Handy for reusing implementation without “is-a”  Leads.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Multiple Inheritance Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
ITM © PortFunctions - 1 ITM 352 Assignment 1 Application Workshop.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 9.
Chapter FifteenModern Programming Languages1 A Second Look At Java.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 6.
Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Chair of Software Engineering Complement to lecture 11 : Levenshtein.
Inheritance Revisited Other Issues. Multiple Inheritance Also called combination--not permitted in Java, but is used in C++ Also called combination--not.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
Object Oriented Programming
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 6.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Lecture 2: Review of Object Orientation. © Lethbridge/La ganière 2005 Chapter 2: Review of Object Orientation What is Object Orientation? Procedural.
Inheritance and Polymorphism
Warm Up Place the following numbers in ascending order, then circle the integers. ½, -2, -12/3, ¾, 0.3, 0, 5/5 Hint: Use your calculator to turn the fractions.
Einführung in die Programmierung Introduction to Programming Prof. Dr
Interfaces.
OOP Paradigms There are four main aspects of Object-Orientated Programming Inheritance Polymorphism Abstraction Encapsulation We’ve seen Encapsulation.
Learning Objectives Inheritance Virtual Function.
MSIS 670 Object-Oriented Software Engineering
Einführung in die Programmierung Introduction to Programming Prof. Dr
Einführung in die Programmierung Introduction to Programming Prof. Dr
Recitation 9 October 28, 2011.
Einführung in die Programmierung Introduction to Programming Prof. Dr
Object Oriented Analysis and Design
Einführung in die Programmierung Introduction to Programming Prof. Dr
COP 3330 Object-oriented Programming in C++
Introduction to Methods and Interfaces
Presentation transcript:

1 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10

2 Today  Multiple inheritance

3 Combining abstractions Given the classes  TRAIN_CAR, RESTAURANT how would you implement a DINER?

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 Examples of multiple inheritance Combining separate abstractions:  Restaurant, train car  Calculator, watch  Other examples? Hands-On

6 Examples of multiple inheritance Combining separate abstractions:  Restaurant, train car  Calculator, watch  Other examples?  Teacher, student  Home, vehicle Hands-On

7 Multiple inheritance: Combining abstractions COMPARABLE NUMERIC STRING COMPLEX INTEGER REAL, >=, … +, –, , / … (total order relation) (commutative ring)

8 Composite figures

9 Multiple inheritance: Composite figures A composite figure Simple figures

10 Defining the notion of composite figure COMPOSITE_ FIGURE center display hide rotate move … count put remove … FIGURE V_LIST [FIGURE ]

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 A composite figure as a list Cursor item forth after before

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 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 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 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 Multiple inheritance: Name clashes f C f A B ? Hands-On

18 Resolving name clashes f rename f as A_f C f A B A_f, f Hands-On

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 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 Feature merging ABC D f + f *f * f *f *  Deferred + Effective

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 Feature merging: effective features ABC D f +  Deferred + Effective -- Undefine f --

24 Undefinition deferred class T inherit S undefine v end feature... end

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 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 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 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 Exercise: All-in-one-device PRINTER ALL_IN_ONE_DEVICE Hands-On SCANNER FAX

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 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 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 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 A special case of multiple inheritance TEACHER STUDENT ASSISTANT UNIVERSITY _MEMBER id This is a case of repeated inheritance ?? ????

35 Indirect and direct repeated inheritance A D B C A D

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 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 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 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 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.