140 likes | 265 Vues
This document explores the fundamental aspects of object-oriented programming (OOP) including the definition and significance of basic types like int, real, bool, and char. It highlights the structure of classes, including examples like `class.Point` and `class.Person`, and discusses issues such as memory wastage and object identity. Additionally, it covers references, cloning, persistence, and the benefits of expanded types for enhanced modeling. Through various programming language comparisons (e.g., Java, C++, Eiffel), this text aims to clarify the principles of object creation, manipulation, and management in software development.
E N D
Objects Run-time Structure and Organization L8Obj
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
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
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
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
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
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
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
Reference Entity Object Entity, Reference, Object • target.feature(args) • If target = null, then an exception will be triggered at run-time. L8Obj
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
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
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
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
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