Chair of Software Engineering OOSC - Summer Semester 2005 1 Bertrand Meyer Object-Oriented Software Construction Lecture 7: Inheritance.

Slides:



Advertisements
Similar presentations
Eiffel: Analysis, Design and Programming Bertrand Meyer (Nadia Polikarpova) Chair of Software Engineering.
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 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 7: References and Assignment.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 3: Abstract Data Types.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
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.
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 Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering Object-Oriented Software Construction Bertrand Meyer Lesson 16 Last update: 25 May 2004 An O-O design example.
Chair of Software Engineering ATOT - Lecture 25, 30 June Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering OOSC Summer Semester Object-Oriented Software Construction Bertrand Meyer.
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 OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 15: Exception handling.
Chair of Software Engineering Object-Oriented Software Construction Bertrand Meyer Lesson 21 Last update: 25 May 2004 Type issues, covariance and catcalls.
Chair of Software Engineering Piotr Nienaltowski, , Concurrent Object-Oriented Programming Bertrand Meyer, Piotr Nienaltowski.
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 Lecture 12: Introduction to inheritance.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
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 OOSC - Lecture 4 1 Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering ATOT - Lecture 9, 30 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 OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 5: Project and EiffelStudio Presentation.
Chair of Software Engineering OOSC - Lecture 21 1 Object-Oriented Software Construction 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 Avoid a void Bertrand Meyer ©Bertrand Meyer, 2008.
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 1 Introduction to Programming Exercise Session Week 9 M. Piccioni 17/18 November 2008.
Chair of Software Engineering ATOT - Lecture 23, 23 June Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 6: Genericity.
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 Object Oriented Software Construction Prof. Dr. Bertrand Meyer Last update: 7 May 2005 Lecture.
Chair of Software Engineering Object-Oriented Software Construction Bertrand Meyer Lecture 21: Agents and tuples.
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 Lecture 12: Introduction to inheritance.
Chair of Software Engineering ATOT - Lecture 7, 23 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 20: Multiple inheritance.
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 10.
Eiffel: Analysis, Design and Programming Bertrand Meyer (Nadia Polikarpova) Chair of Software Engineering.
Chair of Software Engineering ATOT - Lecture 22, 18 June Advanced Topics in Object Technology Bertrand Meyer.
1 Software Architecture Bertrand Meyer ETH Zurich, March-July 2009 Lecture 11: More patterns: Bridge, Composite, Decorator, Façade, Flyweight.
ProgrammingLanguages Programming Languages Object-oriented Programming Languages This lecture discusses the concept of object- oriented programming.
Department of Computer Science, York University Object Oriented Software Construction 14/10/ :15 AM 0 COSC3311 – Software Design Inheritance and.
SQUARE ROOTS AND THEOREM OF PYTHAGORAS REVIEW DAY FOUR.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
1.8: Perimeter, Circumference, and Area
Type == Class  reference type -- store only the object’s address  simple (primitive) type -- for storing small data values Other options Wrapper classes.
Object Oriented Programming
Department of Computer Science, York University Object Oriented Software Construction 22/01/ :58 AM 1 COSC3311 – Software Design Loop invariant/variant.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
9.1 PERIMETER AND AREA OF PARALLELOGRAMS Objective: Students find the perimeter and area of parallelograms.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Einführung in die Programmierung Introduction to Programming Prof. Dr
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Einführung in die Programmierung Introduction to Programming Prof. Dr
Inheritance and Polymorphism OOSC2 chapters 14 and parts of 15-17
Einführung in die Programmierung Introduction to Programming Prof. Dr
Einführung in die Programmierung Introduction to Programming Prof. Dr
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Einführung in die Programmierung Introduction to Programming Prof. Dr
Design by Contract – Exceptions
Presentation transcript:

Chair of Software Engineering OOSC - Summer Semester Bertrand Meyer Object-Oriented Software Construction Lecture 7: Inheritance

Chair of Software Engineering OOSC - Summer Semester Agenda for today  Inheritance  Example  Polymorphism and dynamic binding  Genericity  Assignment attempt

Chair of Software Engineering OOSC - Summer Semester Agenda for today  Inheritance  Example  Polymorphism and dynamic binding  Genericity  Assignment attempt

Chair of Software Engineering OOSC - Summer Semester Example: Inheritance hierarchy FIGURE * OPEN_ FIGURE * CLOSED_ FIGURE * SEGMENTPOLYLINE POLYGON + ELLIPSE CIRCLE RECTANGLE TRIANGLE SQUARE perimeter* perimeter+ perimeter++ diagonal side1 side2 perimeter++ + * deferred + effective ++ redefined extent* center* rotate * display*

Chair of Software Engineering OOSC - Summer Semester Example: POLYGON class POLYGON create make feature vertices: ARRAY [POINT] vertices_count: INTEGER perimeter: REAL is -- Perimeter length do from... until... loop Result := Result + i). distance (i + 1)) end end invariant vertices_count >= 3 vertices_count = vertices.count end i (i + 1)

Chair of Software Engineering OOSC - Summer Semester Example: RECTANGLE by redefining POLYGON class RECTANGLE inherit POLYGON redefine perimeter end create make feature diagonal, side1, side2: REAL perimeter: REAL is -- Perimeter length do Result := 2 * (side1 + side2) end invariant vertices_count = 4 end side1 side2

Chair of Software Engineering OOSC - Summer Semester  Assume: p: POLYGON; r: RECTANGLE; t: TRIANGLE; x: REAL  Permitted: x := p.perimeter x := r.perimeter x := r.diagonal p := r  NOT permitted: x := p.diagonal (even just after p := r !) r := p Polymorphism and dynamic binding (POLYGON) (RECTANGLE) p r 

Chair of Software Engineering OOSC - Summer Semester Polymorphism and dynamic binding  What is the effect of the following (assuming some_test is true)? if some_test then p := r else p := t end x := p.perimeter  Redefinition: A class may change an inherited feature, as with RECTANGLE redefining perimeter of POLYGON.  Polymorphism: p may have different forms at run-time.  Dynamic binding: Effect of p.perimeter depends on run-time form of p.

Chair of Software Engineering OOSC - Summer Semester Dynamic binding: Using non-O-O techniques display (f: FIGURE) is do if ‘‘f is a CIRCLE’’ then... elseif ‘‘f is a POLYGON’’ then... end and similarly for all other routines! Tedious; must be changed whenever there’s a new figure type.

Chair of Software Engineering OOSC - Summer Semester Dynamic binding: in action figure_list: LIST [FIGURE] c: CIRCLE p: POLYGON f: FIGURE figure_list.extend (c) figure_list.extend (p) With: Initialize: and: Then just use: create c.make create p.make create figure_list.make f := figure_list.i_th (i) f.move (…) f.rotate (…) f.display -- and so on for every -- operation on f

Chair of Software Engineering OOSC - Summer Semester The dangers of static binding  For every creation procedure cp: {Pre cp } do cp {Post cp and INV}  For every exported routine r: {INV and Pre r } do r {INV and Post r }  The worst possible erroneous run-time situation in object-oriented software development:  Producing an object that does not satisfy the invariant of its class. a.f (…) a.g (…) a.f (…) create a.make (…) S1 S2 S3 S4

Chair of Software Engineering OOSC - Summer Semester The dangers of static binding  {INV A } do r A {INV A }  {INV B } do r B {INV B }  Consider a call of the form a1.r where a1 is polymorphic:  No guarantee on the effect of do r A on an instance of B! A B rArA r B ++

Chair of Software Engineering OOSC - Summer Semester A concrete example w: WINDOW b: BUTTON create b w := b w.display WINDOW BUTTON display display++

Chair of Software Engineering OOSC - Summer Semester Using original version of redefined feature class BUTTON inherit WINDOW redefine display end feature display is do Precursor {WINDOW} display_border display_label end display_label is do... end display_border is do... end end

Chair of Software Engineering OOSC - Summer Semester Use of Precursor May have arguments. class B inherit A redefine my_feature end feature my_feature (args: SOME_TYPE) is do -- Something here Precursor {A} (args) -- Something else here end A B my_feature my_feature++

Chair of Software Engineering OOSC - Summer Semester Genericity vs. Inheritance LIST_OF_ BOOKS SET_OF_ BOOKS LIKED_LIST_ OF_BOOKS LIST_OF_ PEOPLE LIST_OF_ JOURNALS Abstraction Specialization Type parameterization

Chair of Software Engineering OOSC - Summer Semester Genericity: LIST [G] class LIST [G] feature... last: G is... extend (x: G) is... end figure_list: LIST [FIGURE] r: RECTANGLE s: SQUARE t: TRIANGLE p: POLYGON figure_list.extend (p) figure_list.extend (t) figure_list.extend (s) figure_list.extend (r) figure_list.last.display figure_list

Chair of Software Engineering OOSC - Summer Semester Example: Inheritance hierarchy FIGURE * OPEN_ FIGURE * CLOSED_ FIGURE * SEGMENTPOLYLINE POLYGON + ELLIPSE CIRCLE RECTANGLE TRIANGLE SQUARE perimeter* perimeter+ perimeter++ diagonal side1 side2 perimeter++ + * deferred + effective ++ redefined extent* center* rotate * display*

Chair of Software Engineering OOSC - Summer Semester Genericity: Forcing a type - the problem figure_list.store ("FILE_NAME") Two years later: figure_list := retrieved ("FILE_NAME") x := figure_list.last-- [1] print (x.diagonal)-- [2] But:  If x is declared of type RECTANGLE, [1] is invalid.  If x is declared of type FIGURE, [2] is invalid.

Chair of Software Engineering OOSC - Summer Semester The solution: Assignment attempt x ?= y with x: A  If y is attached to an object whose type conforms to A, perform normal reference assignment.  Otherwise, make x void.

Chair of Software Engineering OOSC - Summer Semester Forcing a type: The solution (using an assignment attempt) f: FIGURE r: RECTANGLE... figure_list := retrieved ("FILE_NAME") f := figure_list.last r ?= f if r /= Void then print (r.diagonal) else print ("Too bad.") end

Chair of Software Engineering OOSC - Summer Semester End of lecture 7