Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 6: Object Creation.

Slides:



Advertisements
Similar presentations
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
Advertisements

Design by Contract.
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 7: References and Assignment.
Chair of Software Engineering Software Architecture Prof. Dr. Bertrand Meyer Lecture 6: Exception Handling.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
CSI 3120, Exception handling, page 1 Exception and Event Handling Credits Robert W. Sebesta, Concepts of Programming Languages, 8 th ed., 2007 Dr. Nathalie.
Software Engineering and Design Principles Chapter 1.
Lecture Roger Sutton CO331 Visual programming 15: Debugging 1.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 2: Dealing with Objects I.
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 Lecture 15: Exception handling.
-5- Exception handling What is an exception? “An abnormal event” Not a very precise definition Informally: something that you don’t want to happen.
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 2: Dealing with Objects I.
Software Testing and Quality Assurance
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 10: Project Presentation Ilinca.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 3.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 4: The Interface of a Class.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 4: The Interface of a Class.
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 Exercise Session 4.
OOP #10: Correctness Fritz Henglein. Wrap-up: Types A type is a collection of objects with common behavior (operations and properties). (Abstract) types.
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.
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 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 6: Object Creation.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 3.
Describing Syntax and Semantics
1 Further OO Concepts II – Java Program at run-time Overview l Steps in Executing a Java Program. l Loading l Linking l Initialization l Creation of Objects.
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 Exercise Session 3.
Ranga Rodrigo. Class is central to object oriented programming.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Chapter 11 Introduction to Classes Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
10 Conversion. Let’s start with conformance Conformance determines when a type may be used in lieu of another. Conformance relies on inheritance. The.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
More About Classes Ranga Rodrigo. Information hiding. Copying objects.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
CSC 480 Software Engineering Design by Contract. Detail Design Road Map Begin with architectural models  Class model: domain classes  Overall state.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 3.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 4.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
Object-Oriented Design Concepts University of Sunderland.
DBC NOTES. Design By Contract l A contract carries mutual obligations and benefits. l The client should only call a routine when the routine’s pre-condition.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
1 Exceptions When the Contract is Broken. 2 Definitions A routine call succeeds if it terminates its execution in a state satisfying its contract A routine.
Chapter 1 The Phases of Software Development. Software Development Phases ● Specification of the task ● Design of a solution ● Implementation of solution.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
Lecture 22 Inheritance Richard Gesick.
Einführung in die Programmierung Introduction to Programming Prof. Dr
Classes, Objects and Methods
CMSC 202 Exceptions.
Presentation transcript:

Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 6: Object Creation

2 Creating objects In previous examples Paris, Line8 etc. denote predefined objects. We’ll now create objects ourselves. Fictitious metro line, fancy_line :

3 class LINE_BUILDING inherit TOUR feature build_a_line -- Build an imaginary line and highlight it on the map. do Paris. display Metro. highlight -- “Create fancy_line and fill in its stations” fancy_line. illuminate end fancy_line : LINE -- An imaginary line of the Metro end Example: LINE_BUILDING Pseudocode Denotes instance of class LINE

4 Identifiers, entities, variables An identifier is a name chosen by the programmer to represent certain program elements It may denote :  A class, e.g. METRO_STATION  A feature, e.g. i_th  A run time value, such as an object or object reference, e.g. fancy_line An identifier that denotes a run-time value is called an entity, or a variable if it can change its value During execution an entity may become attached to an object

5 Entity attached to an object In the program: an entity, such as fancy_line In memory, during execution: an object OBJECT FIELDS fancy_line (LINE ) reference (LINE_BUILDING ) Generating class OBJECT Generating class

6 LINE_BUILDING class LINE_BUILDING inherit TOUR feature build_a_line -- Build an imaginary line and highlight it on the map. do Paris. display Metro. highlight -- “Create fancy_line and fill in its stations” fancy_line. illuminate end fancy_line : LINE -- An imaginary line of the Metro end

7 fancy_line (LINE ) reference (LINE_BUILDING ) Where does this one come from? This object has been created Initial state of a reference In an instance of LINE_BUILDING, may we assume that fancy_line is attached to an instance of LINE?

8 By default Initially, fancy_line is not attached to any object: its value is a void reference OBJECT fancy_line void reference ( LINE_BUILDING )

9 During execution, a reference is either: Attached to a certain object Void States of a reference  To denote a void reference: use the reserved word Void  To find out if x is void, use the condition x = Void  Inverse condition (x is attached to an object): x /= Void

10 The trouble with void references The basic mechanism of computation is feature call x. f (a, …) Since references may be void, x might be attached to no object The call is erroneous in such cases Apply feature f To object to which x is attached Possibly with arguments

11 Example: call on void target Void reference class LINE_BUILDING inherit TOUR feature build_a_line -- Build an imaginary line and highlight it on the map. do Paris. display Metro. highlight -- “Create fancy_line and fill in its stations” fancy_line. illuminate end fancy_line : LINE -- An imaginary line of the Metro end

12 Exceptions Abnormal event during execution. Examples:  “Void call”: fancy_line. illuminate where fancy_line is void  Attempt to compute a / b where b has value 0 A failure will happen unless the program has code to recover from the exception (“rescue” clause in Eiffel, “catch” in Java) Every exception has a type, appearing in EiffelStudio run- time error messages, e.g.  Feature call on void reference (i.e. void call)  Arithmetic underflow

13 Creating objects explicitly To avoid exception:  Change procedure build_a_line to create an object and attach it to fancy_line before call to illuminate

14 Void calls: the full story In ISO Eiffel, will not happen any more thanks to the notion of “attached type”. The compiler rejects any x. f call where x could be void in some execution This is a major improvement but also creates compatibility problem for existing code, so the change is being introduced gradually, starting with EiffelStudio 6.2 Other languages do not have this, but the path was shown by Spec#, a research language from Microsoft Research based on C#, which has “non-null types” In this course we still use the old rules

15 Why do we need to create objects? Shouldn’t we assume that a declaration fancy_line : LINE creates an instance of LINE and attaches it to fancy_line ? (Answer in a little while…)

16 LINE_BUILDING class LINE_BUILDING inherit TOUR feature build_a_line -- Build an imaginary line and highlight it on the map. do Paris. display Metro. highlight -- “Create fancy_line and fill in its stations” fancy_line. illuminate end fancy_line : LINE -- An imaginary line of the Metro end

17 Creating simple objects To create fancy_line, we need to create objects representing stations and stops of the line. Need instances of not only METRO_STATION but STOP (why?) Madeleine? What’s the “next station” after Concorde? Louvre?

18 Stops An instance of STOP has:  A reference to a station; must be non-void.  A reference to next stop; void at the end ( STOP ) right ( METRO_STATION ) station (Void, or to other instance of STOP)

19 Interface of class SIMPLE_STOP class SIMPLE_STOP feature station : METRO_STATION -- Station which this stop represents next : SIMPLE_STOP -- Next stop on the same line set_station (s : METRO_STATION ) -- Associate this stop with s. require station_exists: s /= Void ensure station_set: station = s link (s : SIMPLE_STOP ) -- Make s the next stop on the line. ensure next_set: next = s end

20 LINE_BUILDING stop1 : SIMPLE_STOP -- First stop on the line -- “Create fancy_line and fill in its stations” class LINE_BUILDING inherit TOUR feature build_a_line -- Build an imaginary line and highlight it on the map. do Paris. display Metro. highlight fancy_line. illuminate end fancy_line : LINE -- An imaginary line of the Metro end

21 Creating an instance of SIMPLE_STOP class LINE_BUILDING inherit TOUR feature build_a_line -- Build an imaginary line and highlight it on the map. do Paris. display Metro. highlight fancy_line. illuminate end fancy_line : LINE -- An imaginary line of the Metro end stop1 : SIMPLE_STOP -- First stop on the line -- Create fancy_line and fill in its stations: create stop1 -- “Create more stops and finish building fancy_line” Creation instruction Now an ordinary comment New pseudocode

22 Creating an instance of SIMPLE_STOP class LINE_BUILDING inherit TOUR feature build_a_line -- Build an imaginary line and highlight it on the map. do Paris.display Metro.highlight fancy_line.illuminate end fancy_line : LINE -- An imaginary line of the Paris Metro end

23 Creation instruction Basic operation to produce objects at run time:  Create new object in memory  Attach entity to it stop1 (SIMPLE_STOP) next station create stop1

24 Type of created objects Every entity is declared with a certain type: stop1: SIMPLE_STOP A creation instruction create stop1 produces, at run time, an object of that type.

25 Three stops on a line We now want to add three stops Station_Balard Station_Issy Station_Montrouge First we declare the corresponding attributes: stop1, stop2, stop3 : SIMPLE_STOP

26 build_a_line -- Build an imaginary line and highlight it on the map. do Paris. display Metro. highlight -- Create the stops and associate each to its station: create stop1 stop1. set_station (Station_Montrouge) create stop2 stop2. set_station (Station_Issy) create stop3 stop3. set_station (Station_Balard) -- Link each applicable stop to the next: stop1. link (stop2 ) stop2. link (stop3 ) -- “Create fancy_line and give it the stops just created” fancy_line. illuminate end Predefined from TOURISM Still pseudocode!

27 Let’s ask again: why do we need to create objects? Shouldn’t we assume that a declaration fancy_line : LINE creates an instance of LINE and attaches it to fancy_line ?

28 Void references are useful (PERSON) spouse Married persons:

29 Void references are useful (PERSON) spouse Unmarried person:

30 Void references are useful (PERSON) spouse Even with married persons we shouldn’t create an object for spouse every time we create an instance of PERSON (why?)

31 Using void references (PERSON) spouse Create every PERSON object with a void spouse

32 Using void references (PERSON) spouse Create every PERSON object with a void spouse

33 Using void references (PERSON) spouse Create every PERSON object with a void spouse then attach the spouse references as desired, through appropriate instructions

34 Using void references (PERSON) spouse Create every PERSON object with a void spouse then attach the spouse references as desired, through appropriate instructions

35 References to linked structures To terminate the list, last next reference is void ( STOP ) next ( STOP ) next ( STOP ) next

36 build_a_line -- Build an imaginary line and highlight it on the map. do Paris. display Metro. highlight -- Create the stops and associate each to its station: create stop1 stop1. set_station (Station_Montrouge) create stop2 stop2. set_station (Station_Issy) create stop3 stop3. set_station (Station_Balard) -- Link each applicable stop to the next: stop1. link (stop2 ) stop2. link (stop3 ) -- “Create fancy_line and give it the stops just created” fancy_line. illuminate end Predefined from TOURISM Still pseudocode!

37 create some_stop some_stop. set_station (existing_station) The need for creation procedures Creating and initializing a SIMPLE_STOP object: Invariant of the class: invariant station_exists: station /= Void After creation: invariant not satisfied!

38 Creation procedures A better approach:  Declare set_station as a creation procedure and merge initialization with creation: create new_stop1. set_station (Station_montrouge) -- Same effect as previous two instructions  Convenience: initialize upon creation  Correctness: ensure invariant right from the start Creation procedures are also called constructors

39 Interface of class SIMPLE_STOP class SIMPLE_STOP create set_station feature station : METRO_STATION -- Station which this stop represents next : SIMPLE_STOP -- Next stop on the same line set_station (s : METRO_STATION ) -- Associate this stop with s. require station_exists: s /= Void ensure station_set: station = s link (s : SIMPLE_STOP ) -- Make s the next stop on the line. ensure next_set: next = s end

40 Now doubles up as creation procedure Interface of class STOP class STOP create set_station feature station : METRO_STATION -- Station which this stop represents next : SIMPLE_STOP -- Next stop on the same line set_station (s : METRO_STATION ) -- Associate this stop with s. require station_exists: s /= Void ensure station_set: station = s link (s : SIMPLE_STOP ) -- Make s the next stop on the line. ensure next_set: next = s invariant station_exists : station /= Void end List creation procedures

41 Creation principle This allows the author of the class to force proper initialization of all instances that clients will create. If a class has a non-trivial invariant, it must list one or more creation procedures, whose purpose is to ensure that every instance, upon execution of a creation instruction, will satisfy the invariant

42 Creation procedures Even in the absence of a strong invariant, creation procedures are also useful to combine creation with initialization: class POINT create default_create, make_cartesian, make_polar feature … end Valid creation instructions: create your_point. default_create create your_point create your_point. make_cartesian (x, y) create your_point. make_polar (r, t ) Inherited by all classes, by default does nothing

43 Object creation: summary To create an object:  If class has no create clause, use basic form, create x  If the class has a create clause listing one or more procedures, use create x.make (…) where make is one of the creation procedures, and “(…)” stands for arguments if any

44 Correctness of an instruction For every instruction we must know precisely, in line with the principles of Design by Contract:  How to use the instruction correctly: its precondition.  What we are getting in return: the postcondition. Together, these properties (plus the invariant) define the correctness of a language mechanism. What is the correctness rule for a creation instruction?

45 Correctness of a creation instruction Creation Instruction Correctness Rule Before creation instruction: 1. Precondition of its creation procedure, if any, must hold After creation instruction with target x of type C : 2. x /= Void holds 3. Postcondition of creation procedure holds 4. Object attached to x satisfies invariant of C

46 Successive creation instructions The correctness condition does not require x to be void: create x -- Here x is not void create x x first created object second created object

47 Effect of creation instruction  x won’t be void after creation instruction (whether or not it was void before)  If there is a creation procedure, its postcondition will hold for newly created object  The object will satisfy the class invariant

48 Root creation procedure may:  Create new objects  Call features on them, which may create other objects  Etc. Executing a system consists of creating a root object, which is an instance of a designated class from the system, called its root class, using a designated creation procedure of that class, called its root procedure. How it all starts

49 Root object Root procedure obj1 obj2 r1 r2 create obj1.r1 create obj2.r2 Executing a system

50 Current object At any time during execution, there is a current object, on which the current feature is being executed Initially it is the root object During a “qualified” call x. f (a), the new current object is the one attached to x At the end of such a call, the previous current object resumes its role

51 The design process A system is a particular assembly of certain classes, using one of them as root class. The classes may have value of their own, independently of the system: they may be reusable

52 Extendibility & reusability  Extendibility: the ease with which it is possible to adapt the system to changing user needs  Reusability: the ease of reusing existing software for new applications Older approaches to software engineering, based on the notion of main program and subprograms, pay less attention to these needs

53 Specifying the root How to specify the root class and root creation procedure of a system? Use EiffelStudio

54 What we have seen  Class invariants  Concept of Design by Contract  The notion of exception  Object creation  Creation procedures  Relationship between creation procedures and invariants  The oversall process of system execution