Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chair of Software Engineering OOSC - Summer Semester 2005 1 Bertrand Meyer Object-Oriented Software Construction Lecture 7: Inheritance."— Presentation transcript:

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

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

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

4 Chair of Software Engineering OOSC - Summer Semester 2005 4 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*

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

6 Chair of Software Engineering OOSC - Summer Semester 2005 6 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

7 Chair of Software Engineering OOSC - Summer Semester 2005 7  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 

8 Chair of Software Engineering OOSC - Summer Semester 2005 8 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.

9 Chair of Software Engineering OOSC - Summer Semester 2005 9 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.

10 Chair of Software Engineering OOSC - Summer Semester 2005 10 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

11 Chair of Software Engineering OOSC - Summer Semester 2005 11 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

12 Chair of Software Engineering OOSC - Summer Semester 2005 12 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 ++

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

14 Chair of Software Engineering OOSC - Summer Semester 2005 14 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

15 Chair of Software Engineering OOSC - Summer Semester 2005 15 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++

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

17 Chair of Software Engineering OOSC - Summer Semester 2005 17 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

18 Chair of Software Engineering OOSC - Summer Semester 2005 18 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*

19 Chair of Software Engineering OOSC - Summer Semester 2005 19 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.

20 Chair of Software Engineering OOSC - Summer Semester 2005 20 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.

21 Chair of Software Engineering OOSC - Summer Semester 2005 21 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

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


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

Similar presentations


Ads by Google