Einführung in die Programmierung Introduction to Programming Prof. Dr

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.
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.
Inheritance. class RECTANGLE inherit POLYGON RECTANGLE includes features of POLYGON r: RECTANGLE; p: POLYGON Polymorphism p := r -- legal assignment Dynamic.
Ceg860 (Prasad)L13MI1 Multiple Inheritance DAG Class Structure : Directed Acyclic Graph.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
1 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10.
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 18: Undo/Redo.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 9: Contracts and Inheritance (based on work with.
Eiffel: Analysis, Design and Programming Bertrand Meyer Chair of Software Engineering.
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.
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.
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 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.
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.
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.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 6.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
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.
String and Lists Dr. José M. Reyes Álamo.
Inheritance and Polymorphism
Introduction to the C Language
Number Systems INTRODUCTION.
Introduction To Flowcharting
Einführung in die Programmierung Introduction to Programming Prof. Dr

Object Oriented Practices
Computer Programming.
Object-Oriented and Classical Software Engineering Sixth Edition, WCB/McGraw-Hill, 2005 Stephen R. Schach
OOP Paradigms There are four main aspects of Object-Orientated Programming Inheritance Polymorphism Abstraction Encapsulation We’ve seen Encapsulation.
Learning Objectives Inheritance Virtual Function.
Week 6 Object-Oriented Programming (2): Polymorphism
An Introduction to VEX IQ Programming with Modkit
Programming paradigms
Algorithm Discovery and Design
Einführung in die Programmierung Introduction to Programming Prof. Dr
Einführung in die Programmierung Introduction to Programming Prof. Dr
CISC/CMPE320 - Prof. McLeod
Recitation 9 October 28, 2011.
Einführung in die Programmierung Introduction to Programming Prof. Dr
Object Oriented Analysis and Design
SCIENCE APP REVIEW BY: SARAH SMITH.
Einführung in die Programmierung Introduction to Programming Prof. Dr
Java Programming: From the Ground Up
Object Oriented System Design Class Diagrams
COP 3330 Object-oriented Programming in C++
Lecture 5 Scanning.
Stage 21: Artist: Patterns
Presentation transcript:

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

Today Multiple inheritance

Combining abstractions Given the classes TRAIN_CAR, RESTAURANT how would you implement a DINER? Multiple inheritance is the easy guess, but depending on the circumstances the solution can be fragile, because it is sensitive to changes in the ancestors that can affect the semantics of the descendant in an unpredictable way

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

Examples of multiple inheritance Hands-On Combining separate abstractions: Restaurant, train car Calculator, watch Other examples?

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

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

Composite figures

Multiple inheritance: Composite figures Simple figures A composite figure

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

In the overall structure FIGURE V_LIST [FIGURE ] OPEN_ FIGURE CLOSED_ FIGURE SEGMENT POLYLINE POLYGON ELLIPSE RECTANGLE SQUARE CIRCLE TRIANGLE perimeter+ perimeter* perimeter++ diagonal COMPOSITE_FIGURE 11

A composite figure as a list before after item forth Cursor

Requires dynamic binding 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

An alternative solution: the composite pattern FIGURE LIST [FIGURE ] OPEN_ FIGURE CLOSED_ FIGURE SEGMENT POLYLINE POLYGON ELLIPSE RECTANGLE SQUARE CIRCLE TRIANGLE perimeter+ perimeter* perimeter++ diagonal figure_list COMPOSITE_FIGURE 14

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

Multiple inheritance: Name clashes Hands-On C f A B ? f

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

Consequences of renaming Hands-On 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 Valid Invalid Valid Valid Invalid

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

Feature merging f * A f * B C f + D * Deferred + Effective

Feature merging: with different names class D inherit A rename g as f end B C rename h as f feature ... A g * B f * C h + g f h f D * Deferred + Effective Renaming

Feature merging: effective features B C f + f + f -- f -- D * Deferred + Effective -- Undefine

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

Merging through undefinition A f + B C class D inherit A undefine f end B C undefine f end feature ... end f -- f -- D * Deferred + Effective -- Undefine

Merging effective features with different names B h + C class D inherit A undefine f end B rename g as f undefine f end C h as f feature ... end g f f -- f -- h f D 26

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 27

Exercise: All-in-one-device Hands-On PRINTER SCANNER FAX ALL_IN_ONE_DEVICE

Exercise: All-in-one-device Hands-On class PRINTER feature print_page -- Print a page. do print ("Printer prints a page...") end switch_on -- Switch from ‘off‘ to ‘on‘ print ("Printer switched on...") class FAX send -- Send a page over the phone net. print (“Fax sends a page...") start -- Switch from ‘off‘ to ‘on‘ print (“Fax switched on...") class SCANNER feature scan_page -- Scan a page. do print (“Scanner scans a page...") end switch_on -- Switch from ‘off‘ to ‘on‘ print (“Scanner switched on...") send -- Send data to PC. print (“Scanner sends data...")

Exercise: All-in-one-device Hands-On PRINTER SCANNER FAX ALL_IN_ONE_DEVICE class ALL_IN_ONE_DEVICE inherit ... end Merge switch_on/switch_on/start, as they denote the same concept. Don‘t merge send/send, as they denote different concepts. How to resolve the name clashes? switch_on send

Exercise: All-in-one-device Hands-On class ALL_IN_ONE_DEVICE inherit PRINTER rename switch_on as start undefine start end SCANNER switch_on as start, send as send_data FAX send as send_message feature ... end

Hands-On Valid or invalid? s: SCANNER f: FAX a: ALL_IN_ONE_DEVICE class ALL_IN_ONE_DEVICE inherit PRINTER rename switch_on as start undefine start end SCANNER switch_on as start, send as send_data FAX send as send_message feature ... end 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 Invalid Valid Invalid Valid Valid Invalid

A special case of multiple inheritance UNIVERSITY_MEMBER id TEACHER STUDENT ?? ?? ASSISTANT ???? This is a case of repeated inheritance

Indirect and direct repeated inheritance B Direct repeated inheritance makes sense if you want to redefine features from the ancestor while keeping the ancestor versions. D D

Multiple is also repeated inheritance copy is_equal A typical case: ANY copy ++ is_equal ++ LIST C copy C_copy D is_equal C_is_equal ??

Sharing and replication f g A g g_b g g_c C B D 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.

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 ANY LIST C copy ++ is_equal ++ copy C_copy is_equal C_is_equal D 38

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 39

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.