Unformatted text preview:

ObjectsFields : Structural AspectReferencesObject IdentityEffect of Basic constructorWhy create objects explicitly?Overloading ConstructorsC++ ExampleEntity, Reference, ObjectOperations on ReferencesCloning and CopyingPersistenceComposite Objects : Expanded TypesMotivation for expanded typesceg860(Prasad) L8Obj 1ObjectsRun-time Structure and Organizationceg860(Prasad) L8Obj 2FieldsFields : 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) L8Obj 3References •A referencereference is a run-time value which is either null or attachedattached.•If attachedattached, it identifies a single object.–The concrete representation of a reference may include an address (pointer), type info, etc.•Every object has a unique identityunique identity, independent of its value as defined by its fields.–Object reference can serve as object identity.ceg860(Prasad) L8Obj 4Object 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 stateceg860(Prasad) L8Obj 5Effect 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) L8Obj 6Why 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) L8Obj 7Overloading 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) L8Obj 8C++ Exampleclass 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) L8Obj 9EntityReferenceObject•target.feature(args)–If target = null, then an exceptionexception will be triggered at run-time.Entity, Reference, Objectceg860(Prasad) L8Obj 10Operations 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) 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.ceg860(Prasad) L8Obj 12Persistence•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) L8Obj 13Composite Objects : Expanded TypesEiffel: 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) L8Obj 14Motivation 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


View Full Document

Wright CEG 860 - Objects

Documents in this Course
Load more
Download Objects
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Objects and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Objects 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?