Chair of Software Engineering ATOT - Lecture 7, 23 April 2003 1 Advanced Topics in Object Technology Bertrand Meyer.

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 Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Exercise Session 1: Eiffel Introduction.
Java Review Interface, Casting, Generics, Iterator.
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 1 Introduction to Programming Bertrand Meyer Exercise Session November 2008.
Generic programming in Java
Java Generics.
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.
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 ATOT - Lecture 17, 28 May Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 20: Some design principles.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering ATOT - Lecture 12, 12 May Advanced Topics in Object Technology 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 ATOT - Lecture 16, 26 May Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering OOSC - Lecture 21 1 Object-Oriented Software Construction Bertrand Meyer.
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 ATOT - Lecture 23, 23 June Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering ATOT - Lecture 20, 11 June Advanced Topics in Object Technology Bertrand Meyer.
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 6: Genericity.
Chair of Software Engineering ATOT - Lecture 3, 7 April Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering ATOT - Lecture 14, 19 May Advanced Topics in Object Technology Bertrand Meyer.
Static and Dynamic Behavior Fall 2005 OOPD John Anthony.
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.
1 L40 Generics (2). 2 OBJECTIVES  To understand raw types and how they help achieve backwards compatibility.  To use wildcards when precise type information.
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.
Chair of Software Engineering ATOT - Lecture 22, 18 June Advanced Topics in Object Technology Bertrand Meyer.
1 Genericity Parameterizing by Type. 2 Generic Class One that is parameterized by type  Works when feature semantics is common to a set of types On object.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
Ceg860 (Prasad)L6MR1 Modularity Extendibility Reusability.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Generics and Collections. Introduction Generics New feature of J2SE 5.0 Provide compile-time type safety Catch invalid types at compile time Generic methods.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Genericity Ranga Rodrigo Based on Mark Priestley's Lectures.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
CMSC 330: Organization of Programming Languages Java Generics.
Chapter 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms Design Issues.
CSCI-383 Object-Oriented Programming & Design Lecture 24.
ISBN Chapter 10 Implementing Subprograms.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
CSC 520 – Advanced Object Oriented Programming, Fall, 2010 Thursday, September 30 Week 5, Generics and Inheritance Techniques, Meyer Ch. 10 & 16.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
Chapter 9 Subprograms.
Type Checking and Type Inference
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Einführung in die Programmierung Introduction to Programming Prof. Dr
CSCI 3370: Principles of Programming Languages Chapter 9 Subprograms
Einführung in die Programmierung Introduction to Programming Prof. Dr
Einführung in die Programmierung Introduction to Programming Prof. Dr
Einführung in die Programmierung Introduction to Programming Prof. Dr
Presentation transcript:

Chair of Software Engineering ATOT - Lecture 7, 23 April Advanced Topics in Object Technology Bertrand Meyer

Chair of Software Engineering ATOT - Lecture 7, 23 April Lecture 7: Genericity

Chair of Software Engineering ATOT - Lecture 7, 23 April Genericity  Parameterized classes for static typing  Examples: stacks, arrays  Constrained genericity (preview)

Chair of Software Engineering ATOT - Lecture 7, 23 April Extending the basic notion of class LIST_OF_ BOOKS SET_OF_ BOOKS LINKED_LIST _OF_BOOKS LIST_OF_ PEOPLE LIST_OF_ JOURNALS Abstraction Specialization Type parameterization

Chair of Software Engineering ATOT - Lecture 7, 23 April Genericity: Ensuring type safety  How can we define consistent “container” data structures, e.g. stack of accounts, stack of points?  Dubious use of a container data structure: a: ACCOUNT p, q: POINT point_stack: STACK... account_stack: STACK... point_stack.put (p) account_stack.put (a) q := point_stack.item q.move (3.4, 1.2)

Chair of Software Engineering ATOT - Lecture 7, 23 April Possible approaches  Ignore the problem until run-time. Possible “feature not available” errors. This is the dynamic typing approach of Smalltalk and Java.  Use a pseudo-universal type, such as “pointer to void” or “pointer to character” in C, to represent G in the class, and cast everything to that type.  Duplicate code, manually or with the help of macro processor.  Parameterize the class, giving a name G to the type of container elements; the features of the class may refer to type G. This is the Eiffel approach. (See also C++ templates.)

Chair of Software Engineering ATOT - Lecture 7, 23 April A generic class class STACK [G] feature put (x: G) is... item: G is... end  To use the class: obtain a generic derivation, e.g. account_stack: STACK [ACCOUNT] Formal generic parameter Actual generic parameter

Chair of Software Engineering ATOT - Lecture 7, 23 April Conformance rule  B [U] conforms to A [T] if and only if B is a descendant of A and U conforms to T.

Chair of Software Engineering ATOT - Lecture 7, 23 April Using generic derivations account_stack: STACK [ACCOUNT] point_stack: STACK [POINT] a: ACCOUNT p, q, r: POINT... point_stack.put (p) point_stack.put (q) r := point_stack.item r. move (3.0, –5.0) account_stack.put (a)...

Chair of Software Engineering ATOT - Lecture 7, 23 April Genericity and static typing  Compiler will reject point_stack.put (a) account_stack.put (p)  To define more flexible data structures (e.g. stack of figures): use inheritance, polymorphism and dynamic binding.

Chair of Software Engineering ATOT - Lecture 7, 23 April Typing is in O-O context  An object-oriented language is statically typed if and only if it is possible to write a static checker which, if it accepts a system, guarantees that at run time, for any execution of a feature call x.f, the object attached to x (if any) will have at least one feature corresponding to f.

Chair of Software Engineering ATOT - Lecture 7, 23 April Constrained genericity class VECTOR [G] feature infix "+" (other: VECTOR [G]): VECTOR [G] is -- Sum of current vector and other require lower = other.lower upper = other.upper local a, b, c: G do... See next... end... Other features... end

Chair of Software Engineering ATOT - Lecture 7, 23 April Constrained genericity (cont’d)  The body of infix "+": create Result.make (lower, upper) from i := lower until i > upper loop a := item (i) b := other.item (i) c := a + b-- Requires a “+” operation on G! Result.put (c, i) i := i + 1 end

Chair of Software Engineering ATOT - Lecture 7, 23 April The solution  Declare class VECTOR as class VECTOR [G –> NUMERIC] feature... The rest as before... end  Class NUMERIC (from the Kernel Library) provides features infix "+", infix "*" and so on.  Better yet: make VECTOR itself a descendant of NUMERIC, effecting the corresponding features:

Chair of Software Engineering ATOT - Lecture 7, 23 April Improving the solution  Make VECTOR itself a descendant of NUMERIC, effecting the corresponding features: class VECTOR [G –> NUMERIC] inherit NUMERIC feature... The rest as before, including infix "+"... end  Then it is possible to define e.g. v: VECTOR [VECTOR [VECTOR [INTEGER]]]

Chair of Software Engineering ATOT - Lecture 7, 23 April A generic library class: Arrays  Using arrays in a client a: ARRAY [REAL]... create a.make (1, 300) a.put (3.5, 25) -- (in Pascal: a[25] := 3.5) x := a.item (i) -- (in Pascal: x := a[i]) -- Alternatively: x := i -- Using the function infix  Also: ARRAY2 [G] etc.

Chair of Software Engineering ATOT - Lecture 7, 23 April The ARRAY class class ARRAY [G] create make feature lower, upper: INTEGER count: INTEGER make (min: INTEGER, max: INTEGER) is -- Allocate array with bounds min and max. do... end

Chair of Software Engineering ATOT - Lecture 7, 23 April The ARRAY class (cont’d) item, infix (i: INTEGER): G is -- Entry of index i require lower <= i i <= upper do... end put (v: G; i: INTEGER) is -- Assign the value of v to the entry of index i. require lower <= i i <= upper do... end invariant count = upper – lower + 1 end

Chair of Software Engineering ATOT - Lecture 7, 23 April Obsolete features and classes  How to reconcile progress with the protection of the installed base?  Obsolete features and classes support smooth evolution.  In class ARRAY: enter (i: V; v: T) is obsolete "Use `put (value, index)’" do put (v, i) end

Chair of Software Engineering ATOT - Lecture 7, 23 April Obsolete classes class ARRAY_LIST [G] obsolete "[ Use MULTI_ARRAY_LIST instead (same semantics, but new name ensures more consistent terminology). Caution: do not confuse with ARRAYED_LIST (lists implemented by one array each). ]" inherit MULTI_ARRAY_LIST [G] end

Chair of Software Engineering ATOT - Lecture 7, 23 April End of lecture 7