ceg860(Prasad)L8Obj1 Objects Run-time Structure and Organization
ceg860(Prasad)L8Obj2 Fields Fields : Structural Aspect Basic Types ( int, real, bool, char, etc.) –E.g., class Point { real x, y; } Sub-objects –E.g, class Person { String name; …} class Car { Person owner; …} Wastes memory space. Fails to express sharing of objects : Update consistency problem.
ceg860(Prasad)L8Obj3 References reference attachedA reference is a run-time value which is either null or attached. attachedIf attached, it identifies a single object. –The concrete representation of a reference may include an address (pointer), type info, etc. unique identityEvery object has a unique identity, independent of its value as defined by its fields. –Object reference can serve as object identity.
ceg860(Prasad)L8Obj4 Object Identity Two objects with different identities may have identical fields. (Cf. Java == vs equal ) (Cf. Scheme’s eq vs equal ) (Cf. Ada’s limited private type ) (Cf. Eiffel’s = vs equal vs deep_equal ) Conversely, the fields of a certain object may change during the execution; but this does not affect the object’s identity. –Objects with state
ceg860(Prasad)L8Obj5 Effect of Basic constructor Cl var; Cl var = new Cl(); Create a new instance of Cl called nCl. Initialize each field of nCl using standard default values. Int : 0, Class : null, bool : false, etc Attach ncl to var (a reference).
ceg860(Prasad)L8Obj6 Why create objects explicitly? Implicit creation highly inefficient (impossible for recursive structures). –Self-reference implies non-termination. Realistic modeling can require null reference or values of two fields to be attached to the same object (sharing). –Representing Relations and Functions.
ceg860(Prasad)L8Obj7 Overloading Constructors To override language-defined defaults. Java/C++Java/C++ : Signature-based resolution. EiffelEiffel: Named constructors. Ensure object satisfies consistency constraints imposed by ADT spec. E.g., Age = Year of Birth Current Year Support for programmer defined initializations. E.g., Label, Color, Location, etc of a Button.
ceg860(Prasad)L8Obj8 C++ Example class Complex { public: Complex(double); Complex(); private: double re, im; } Complex pi = 3.14; Complex e (2.71); Complex:: Complex ( double r){ re = r; im = 0; } Complex:: Complex( double r): re(r), im(0) {}
ceg860(Prasad)L8Obj9 Entity Reference Object target.feature(args) exception –If target = null, then an exception will be triggered at run-time. Entity, Reference, Object
ceg860(Prasad)L8Obj10 Operations on References Reference Assignment ( Cl x = y; ) –Sharing of objects (Reattachment) : Entities x and y now refer to the same object. Dynamic aliasing. –Garbage Collection (Recycling) : If the object, referred to by x initially, becomes unattached (“unreachable”) now, it may be recycled. Reference Comparison –Equal (==) and Not Equal (=/=)
ceg860(Prasad)L8Obj11 Cloning and Copying x.clone(y) x is attached to an object (say, OX) that is a duplicate of the object attached to y (say, OY). The corresponding fields of OX and OY have identical values. Shallow copy : clone is not recursive. –Deep cloning duplicates a structure recursively without introducing any sharing of references. x.copy(y) Both x and y must be non-null.
ceg860(Prasad)L8Obj12 Persistence Motivation: Communication with other systems –Read/Write objects from/to files, databases, and other devices. In practice: –Require a mechanism to store and retrieve objects containing references. Java 1.1 Serialization and ObjectStreams Eiffel: class Storable –Require context to determine the type of the retrieved object.
ceg860(Prasad)L8Obj13 Composite Objects : Expanded Types Eiffel: expanded Cl x; vs Cl y; C++: Cl x; vs Cl* y; The value of entity x is an instance of Cl. Available in C++ and Ada-95. Not available in Java. The value of entity y is a reference to an instance of Cl. Available in Java, C++, and Ada-95.
ceg860(Prasad)L8Obj14 Motivation for expanded types Enhanced modeling power. Class WorkStation { k: expanded KeyBoard; c: expanded CPU; m: expanded Monitor; n: Network; } –Every instance of Workstation has (contains) an instance of CPU. (Not shared.) Can improve (space and time) efficiency. –Cyclic dependency prohibited.