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

Slides:



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

Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
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.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
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.
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 12: More on references and.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 15: Exception handling.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
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 6: Object Creation.
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 Lecture 9: Abstraction.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering OOSC - Lecture 21 1 Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering ATOT - Lecture 25, 30 June Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering Avoid a void Bertrand Meyer ©Bertrand Meyer, 2008.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 12: More on references and.
Chair of Software Engineering ATOT - Lecture 23, 23 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 Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 6: Object Creation.
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 5.
Chair of Software Engineering ATOT - Lecture 5, 14 April Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering ATOT - Lecture 7, 23 April Advanced Topics in Object Technology Bertrand Meyer.
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Abstract Data Types and Encapsulation Concepts
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 9: Abstraction.
Ranga Rodrigo. Class is central to object oriented programming.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
10 Conversion. Let’s start with conformance Conformance determines when a type may be used in lieu of another. Conformance relies on inheritance. The.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
© 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.
Programming Languages by Ravi Sethi Chapter 6: Groupings of Data and Operations.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
Department of Computer Science, York University Object Oriented Software Construction 30/10/ :54 PM 0 COSC3311 – Software Design Objects, Classes.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Object-Oriented Programming Chapter Chapter
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Ceg860(Prasad)L8Obj1 Objects Run-time Structure and Organization.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
ISBN Chapter 12 Support for Object-Oriented Programming.
Static data members Constructors and Destructors
Abstract Data Types and Encapsulation Concepts
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
11.1 The Concept of Abstraction
Objects, Classes and ADTs Reference vs. Expanded Types
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
11.1 The Concept of Abstraction
Lecture 9 Concepts of Programming Languages
Presentation transcript:

Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 4: Objects

Chair of Software Engineering OOSC - Summer Semester The basic structure: The class  A class is an implementation of an ADT. It is both:  A module.  A type.  Much of the conceptual power of the method comes from the fusion of these two notions.

Chair of Software Engineering OOSC - Summer Semester The basic structure: The class (cont’d)  From the module viewpoint:  Set of available services (“features”).  Information hiding.  Classes may be clients of each other.  From the type viewpoint:  Describes a set of run-time objects (the instances of the class).  Used to declare entities ( variables), e.g. x: C  Possible type checking.  Notion of subtype.

Chair of Software Engineering OOSC - Summer Semester Avoid “objectspeak”  The run-time structures, some of them corresponding to “objects” of the modeled system, are objects.  The software modules, each built around a type of objects, are classes.  A system does not contain any “objects” (although its execution will create objects).

Chair of Software Engineering OOSC - Summer Semester Terminology  A class is an implementation of an abstract data type.  Instances of the class may be created at run-time; they are objects.  Every object is an instance of a class. (In a pure O-O language such as Eiffel and Smalltalk this is true even of basic objects such as integers etc. Not true in C++ or Java where such values have special status.)  A class is characterized by features. Features comprise attributes (representing data fields of instances of the class) and routines (operations on instances).  Routines are subdivided into procedures (effect on the instance, no result) and functions (result, normally no effect).  Every operation (routine or attribute call) is relative to a distinguished object, the current instance of the class.

Chair of Software Engineering OOSC - Summer Semester Feature categories by role Command Query Feature Procedure Attribute Function No result Returns result Computation Memory

Chair of Software Engineering OOSC - Summer Semester Feature categories by implementation Procedure Attribute Function Routine Returns result No result Feature Memory Computation

Chair of Software Engineering OOSC - Summer Semester Feature categories Command Query Feature Procedure Attribute Function No result Returns result Computation Memory Routine Returns result No result Feature Memory Computation

Chair of Software Engineering OOSC - Summer Semester Alternative terminology  Attributes are also called instance variables or data member.  Routines are also called methods, subprograms, or subroutines.  Feature call — applying a certain feature of a class to an instance of that class — is also called passing a message to that object.  The notion of feature is particularly important as it provides a single term to cover both attributes and routines. It is often desirable not to specify whether a feature is an attribute or a routine — as expressed by the Uniform Access principle (see next).

Chair of Software Engineering OOSC - Summer Semester Uniform Access balance = list_of_deposits.total – list_of_withdrawals.total list_of_deposits list_of_withdrawals balance list_of_deposits list_of_withdrawals (A2) (A1)

Chair of Software Engineering OOSC - Summer Semester The Principle of Uniform Access  Facilities managed by a module must be accessible to clients in the same way whether implemented by computation or storage.

Chair of Software Engineering OOSC - Summer Semester Uniform access through feature call  To access a property of a point p1, the notation is the same regardless of the representation, e.g. p1.x which is applicable both in cartesian representation (x is an attribute) and in polar representation (x is a function without arguments).  In the first case the feature call is a simple field access; in the second it causes a computation to be performed.  There is no difference for clients (except possibly in terms of performance).

Chair of Software Engineering OOSC - Summer Semester Abstract data type POINT x: POINT  REAL y: POINT  REAL : POINT  REAL : POINT  REAL  Class POINT: Choose a representation (polar, cartesian)  In polar representation,  and  are attributes, x and y are routines. y x  

Chair of Software Engineering OOSC - Summer Semester Class POINT class POINT feature x, y: REAL -- Point cartesian coordinates move (a, b: REAL) is -- Move by a horizontally and by b vertically. do x := x + a y := y + b end scale (factor: REAL) is -- Scale by factor. do x := factor * x y := factor * y end

Chair of Software Engineering OOSC - Summer Semester Class POINT (continued) distance (p: POINT): REAL is -- Distance to p do Result := sqrt ((x – p.x)^2 + (y – p.y)^2) end ro: REAL is -- Distance to origin (0, 0) do Result := sqrt (x^2 + y^2) end theta: REAL is -- Angle to horizontal axis do … end

Chair of Software Engineering OOSC - Summer Semester Use of the class in a client class GRAPHICS feature p, q: POINT -- Graphic points … some_routine is -- Use p and q. local u, v: REAL do -- Creation instructions create p end 0.0 p (POINT)

Chair of Software Engineering OOSC - Summer Semester Use of the class in a client class GRAPHICS feature p, q: POINT -- Graphic points … some_routine is -- Use p and q. local u, v: REAL do -- Creation instructions create p create q end 0.0 p (POINT) 0.0 q (POINT)

Chair of Software Engineering OOSC - Summer Semester Use of the class in a client class GRAPHICS feature p, q: POINT -- Graphic points … some_routine is -- Use p and q. local u, v: REAL do -- Creation instructions create p create q p.move (4.0, -2.0) -- Compare with Pascal, C, Ada: -- move (p, 4.0, -2.0) end p (POINT) 0.0 q (POINT)

Chair of Software Engineering OOSC - Summer Semester Use of the class in a client class GRAPHICS feature p, q: POINT -- Graphic points … some_routine is -- Use p and q. local u, v: REAL do -- Creation instructions create p create q p.move (4.0, -2.0) -- Compare with Pascal, C, Ada: -- move (p, 4.0, -2.0) p.scale (0.5) end 2.0 p (POINT) 0.0 q (POINT)

Chair of Software Engineering OOSC - Summer Semester Use of the class in a client class GRAPHICS feature p, q: POINT -- Graphic points … some_routine is -- Use p and q. local u, v: REAL do -- Creation instructions create p create q p.move (4.0, -2.0) -- Compare with Pascal, C, Ada: -- move (p, 4.0, -2.0) p.scale (0.5) u := p.distance (q) v := p.x p := q end 2.0 p (POINT) 0.0 q (POINT)

Chair of Software Engineering OOSC - Summer Semester Use of the class in a client class GRAPHICS feature p, q: POINT -- Graphic points … some_routine is -- Use p and q. local u, v: REAL do -- Creation instructions create p create q p.move (4.0, -2.0) -- Compare with Pascal, C, Ada: -- move (p, 4.0, -2.0) p.scale (0.5) u := p.distance (q) v := p.x p := q p.scale (-3.0) end 2.0 p (POINT) 0.0 q (POINT)

Chair of Software Engineering OOSC - Summer Semester Variants of assignment and copy  Reference assignment (a and b of reference types): b := a  Object duplication (shallow): c := clone (a)  Object duplication (deep): d := deep_clone (a)  Also: shallow field-by-field copy (no new object is created): e.copy (a)

Chair of Software Engineering OOSC - Summer Semester Shallow and deep cloning Initial situation: Result of: b := a c := clone (a) d := deep_clone (a) “Almaviva” name landlord loved_one O1 “Figaro” O2 “Susanna” O3 b “Almaviva” O4 c “Almaviva” O5 “Figaro” O6 “Susanna” O7 d a

Chair of Software Engineering OOSC - Summer Semester Where do these mechanisms come from?  Class ANY in the Eiffel “Kernel Library”  Every class that doesn’t explicitly inherit from another is considered to inherit from ANY  As a result, every class is a descendant of ANY.

Chair of Software Engineering OOSC - Summer Semester Completing the inheritance structure A BDEC ANY NONE Inherits from

Chair of Software Engineering OOSC - Summer Semester A related mechanism: Persistence a.store (file).... b ?= retrieved (file)  Storage is automatic.  Persistent objects identified individually by keys.  These features come from the library class STORABLE.

Chair of Software Engineering OOSC - Summer Semester Applying abstraction principles  Privileges of a client C of a class A on an attribute attrib:  Read access if attribute is exported.  Assuming a1: A Then a1.attrib is an expression.  An assignment such as a1.attrib := a2 is syntactically illegal! (You cannot assign a value to an expression, e.g. x + y.) CA a1: A

Chair of Software Engineering OOSC - Summer Semester The privileges of a client Secret Read-only Read, restricted write Full write

Chair of Software Engineering OOSC - Summer Semester Applying abstraction principles  Beyond read access: full or restricted write, through exported procedures.  Full write privileges: set_attribute procedure, e.g. set_temperature (u: REAL) is -- Set temperature value to u. do temperature := u ensure temperature_set: temperature = u end  Client will use e.g. x.set_temperature (21.5).

Chair of Software Engineering OOSC - Summer Semester Setter procedures  set_attribute procedure, e.g. set_temperature (u: REAL) is -- Set temperature value to u. do temperature := u ensure temperature_set: temperature = u end  Client will use e.g. x.set_temperature (21.5).  Client cannot directly assign to attribute

Chair of Software Engineering OOSC - Summer Semester Other uses of a setter procedure set_temperature (u: REAL) is -- Set temperature value to u. require not_under_minimum: u >= -273 not_above_maximum: u <= 2000 do temperature := u update_database (u, Current) ensure temperature_set: temperature = u end

Chair of Software Engineering OOSC - Summer Semester Delphi/C# “properties”  Allow x.temperature = 21.5; if there is a “setter”: private int temperature_internal; public int temperature { get {return temperature_internal; } set { temperature_internal = value; //... Other instructions;... }

Chair of Software Engineering OOSC - Summer Semester Information hiding class A feature f... g... feature {NONE} h... feature {B, C} j... feature {A, B, C} k end In clients, with the declaration a1: A, we have:  a1.f, a1.g: valid in any client  a1.h: invalid anywhere (including in A’s own text).  a1.j: valid only in B, C and their descendants (not valid in A!)  a1.k: valid in B, C and their descendants, as well as in A and its descendants

Chair of Software Engineering OOSC - Summer Semester Information hiding  Information hiding only applies to use by clients, using dot notation or infix notation, as with a1.f (“Qualified calls”).  Unqualified calls (within the class itself) are not subject to information hiding: class A feature {NONE} h is -- Does something. do... end feature f is -- Use h. do... h end end

Chair of Software Engineering OOSC - Summer Semester The dynamic model  States of a reference:  Operations on references: create p p := q p := Void if p = Void then... VOIDATTACHED create p p := q (where q is attached) p := Void p := q (where q is void) p ATTACHED p VOID

Chair of Software Engineering OOSC - Summer Semester Creating an object  With the class POINT as given: my_point: POINT... create my_point  Effect of such a creation instruction:  Allocate new object of the type declared for my_point.  Initialize its fields to default values (0 for numbers, false for booleans, null for characters, void for references).  Attach it to the instruction’s target, here my_point.

Chair of Software Engineering OOSC - Summer Semester Specific creation procedures class POINT create make_cartesian, make_polar feature {NONE} -- Initialization make_cartesian (a, b: REAL) is -- Initialize to abscissa a, ordinate b. do x := a y := b end make_polar... feature... The rest as before...

Chair of Software Engineering OOSC - Summer Semester If there is a creation clause  Creation instructions must be “creation calls”, such as create my_point.make_polar (1, Pi / 2)

Chair of Software Engineering OOSC - Summer Semester If there is no creation clause  An absent creation clause, as in class POINT -- No creation clause feature … The rest as before … end is understood as one that would only list default_create, as if it had been written class POINT create default_create feature … The rest as before … end  Procedure default_create is defined in ANY as doing nothing; any class can redefine it to provide proper default initializations.

Chair of Software Engineering OOSC - Summer Semester Associated convention  The notation create x is understood (if permitted) as an abbreviation for create x.default_create

Chair of Software Engineering OOSC - Summer Semester To allow both forms  To make both forms valid: create my_point as well as create my_point.make_polar (1, Pi / 2) it suffices to make default_create (redefined or not) one of the creation procedures: class POINT create make_cartesian, make_polar, default_create feature... The rest as before...

Chair of Software Engineering OOSC - Summer Semester What to do with unreachable objects  Reference assignments  may make some objects useless.  Two possible approaches:  Manual reclamation (e.g. C++, Delphi).  Automatic garbage collection (e.g. Eiffel, Smalltalk, Simula, Java,.NET) “Almaviva”name landlord loved_one a O1 “Figaro” O2 “Susanna” O3

Chair of Software Engineering OOSC - Summer Semester Arguments for automatic collection  Manual reclamation is dangerous. Hampers software reliability.  In practice bugs arising from manual reclamation are among the most difficult to detect and correct. Manifestation of bug may be far from source.  Manual reclamation is tedious: need to write “recursive dispose” procedures.  Modern garbage collectors have acceptable overhead (a few percent) and can be made compatible with real-time requirement.  GC is tunable: disabling, activation, parameterization....

Chair of Software Engineering OOSC - Summer Semester Properties of a garbage collector (GC)  Consistency (never reclaim a reachable object).  Completeness (reclaim every unreachable object – eventually).  Consistency (also called safety) is an absolute requirement. Better no GC than an unsafe GC.  But: safe automatic garbage collection is hard or impossible in a hybrid language environment (e.g. C++): pointers may masquerade as integers or other values.

Chair of Software Engineering OOSC - Summer Semester Types  Reference types; value of an entity is a reference. Example: b: POINT  Expanded types; value of an entity is an object. Example: d: expanded POINT b (POINT) d

Chair of Software Engineering OOSC - Summer Semester Expanded classes  A class may also be declared as expanded class C... The rest as usual...  Then you can declare: a: C with the same effect as b: expanded C in the earlier syntax (still permitted, with same meaning).

Chair of Software Engineering OOSC - Summer Semester Subobjects  Expanded classes and entities support the notion of subobject. class RECTANGLE_R feature corner1, corner2, corner3, corner4: POINT... end class RECTANGLE_E feature corner1, corner2, corner3, corner4: expanded POINT … end

Chair of Software Engineering OOSC - Summer Semester The meaning of expanded classes  More than an implementation notion: a system modeling tool.  Two forms of client relation:  Simple client  Expanded client  What is the difference between these two statements?  A car has an originating factory.  A car has an engine.

Chair of Software Engineering OOSC - Summer Semester Basic types as expanded classes expanded class INTEGER... expanded class BOOLEAN... expanded class CHARACTER... expanded class REAL... expanded class DOUBLE... n: INTEGER

Chair of Software Engineering OOSC - Summer Semester Infix and prefix operators expanded class INTEGER feature infix "+" (other: INTEGER): INTEGER is -- Sum with other do... end infix "*" (other: INTEGER): INTEGER is -- Product by other do... end prefix "–": INTEGER is -- Unary minus do... end... end Calls are then of the form i + j rather than i.plus (j).

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