1 / 14

Objects

Objects. Run-time Structure and Organization. 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.

frisco
Download Presentation

Objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Objects Run-time Structure and Organization L8Obj

  2. 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. L8Obj

  3. References • A reference is a run-time value which is either null or attached. • If attached, it identifies a single object. • The concrete representation of a reference may include an address (pointer), type info, etc. • Every object has a unique identity, independent of its value as defined by its fields. • Object reference can serve as object identity. L8Obj

  4. Object Identity • Two objects with different identities may have identical fields. • (Cf. Java ==vsequal) • (Cf. Scheme’s eqvsequal) • (Cf. Ada’s limited private type) • (Cf. Eiffel’s =vs equalvsdeep_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 L8Obj

  5. 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). L8Obj

  6. 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. L8Obj

  7. Overloading Constructors • To override language-defined defaults. • Java/C++ : Signature-based resolution. • Eiffel: 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. L8Obj

  8. 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) {} C++ Example L8Obj

  9. Reference Entity Object Entity, Reference, Object • target.feature(args) • If target = null, then an exception will be triggered at run-time. L8Obj

  10. 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 (=/=) L8Obj

  11. 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. L8Obj

  12. 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: classStorable • Require context to determine the type of the retrieved object. L8Obj

  13. Composite Objects : Expanded Types Eiffel:expanded Cl x; vs Cl y; C++: Cl x; vs Cl* y; • The value of entity xis an instance ofCl. • Available in C++ and Ada-95. • Not available in Java. • The value of entityy isa reference to an instance ofCl. • Available in Java, C++, and Ada-95. L8Obj

  14. 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. L8Obj

More Related