Objects - PowerPoint PPT Presentation

About This Presentation
Title:

Objects

Description:

Fails to express sharing of objects : Update consistency problem. ceg860(Prasad) L8Obj ... A reference is a run-time value which is either null or attached. ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 15
Provided by: csWr
Learn more at: http://cecs.wright.edu
Category:
Tags: awww | objects

less

Transcript and Presenter's Notes

Title: Objects


1
Objects
  • Run-time Structure and Organization

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.

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.

4
Object Identity
  • Two objects with different identities may have
    identical fields.
  • (Cf. Java vs equal)
  • (Cf. Schemes eq vs equal)
  • (Cf. Adas limited private type)
  • (Cf. Eiffels vs equal vs deep_equal)
  • Conversely, the fields of a certain object may
    change during the execution but this does not
    affect the objects identity.
  • Objects with state

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

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.

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.

8
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)

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

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 (/)

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.

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 class Storable
  • Require context to determine the type of the
    retrieved object.

13
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.

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.
Write a Comment
User Comments (0)
About PowerShow.com