200 likes | 319 Views
Implications of Substitution. CMPS 2143. Overview. Memory Layout and methods of space allocation Assignment Copies and Clones Equality and Identity. Inheritance and Substitution. Subtle, yet pervasive impact on MANY aspects of a programming language Type system
E N D
Implications of Substitution CMPS 2143
Overview • Memory Layout and methods of space allocation • Assignment • Copies and Clones • Equality and Identity
Inheritance and Substitution • Subtle, yet pervasive impact on MANY aspects of a programming language • Type system • Meaning of assignment, testing for equivalence • Creation of copies • Storage allocation • Is-a relationship: associate a type (of a variable) with a set of values • Window win = new Window(); • Window win = new TextWindow();
Storage allocation • Commonly believed variables allocated on the run-time stack (in activation records) are more efficient than variables allocated on the heap. • Language designers go to great lengths to make this happen • Major problem: storage requirements must be determined at statically (at compile-time) or at least at procedure-entry time. • Values the variables hold are determined at runtime.
Subclasses and storage requirements • Subclasses may have more data (not present in superclass) • Ex. textWindow may have more data than window, • eg. a string and cursorLocation • When allocating for a Window object, should we really allocate for a TextWindow object, just in case?
THREE SOLUTIONS • Minimum space allocation: Allocate the amount of space necessary for the base class. • Maximum space allocation: Allocate the maximum amount of space necessary to hold any legal value (from any of the classes in the hierarchy) • Dynamic memory allocation: Allocate only the amount of space necessary to hold a single pointer/reference. (Allocate the space necessary for the value at runtime on the heap.)
Minimum space allocation • C was designed to be efficient, C++ retains many concepts of nondynamic and dynamic variables Window win; //puts an object on the stack Window * tWinPtr; //puts an pointer on the stack tWinPtr = new TextWindow(); //allocates on the heap • What happens if …? win = *tWinPtr; • Space allocated to win on the stack is not large enough!!!
Minimum Space Allocation • Default: not all fields copied/only the corresponding fields. (termed slicing) • C++ can override the meaning of assignment, but how without losing some information? • Do we care? • Can only call methods in win (Window) • What if methods are overridden in the subclass and they access extra data?
Minimum Space Allocation • C++: 2 solutions • For pointers (and references) – member function selected is determined by dynamic value of receiver • For other variables – binding determined by static class (the class at declaration) • Given this modified rule – not possible for a method to access fields that are physically present in memory in the object
Maximum Space Allocation • Might seem to be ideal (although possibly wasteful) • Size of object must be known by scanning ENTIRE program • Need 2 pass compiler • Need to see all libraries, linked in code, etc. • No major programming language uses this approach
Dynamic Memory Allocation • Values of objects not stored on the stack – only the pointer or reference is • Values stored in the heap. • Java, Smalltalk, Object Pascal, Objective-C use this approach • Must use new to allocate space in the heap. • Will have consequences with assignment semantics!!!
Garbage collection • When data stored in heap, must be recovered • Use delete in C++ • Use auto garbage collector
Assignment – 2 interpretations • Copy semantics – assignment copies entire value of right side, assigning it to left, thereafter the two values are independent (changes in one not reflected in other) • Sometimes used in C++, sometimes not • Pointer semantics – Assignment changes the reference of left side to be of right side (aliasing). Two pointers refer to the same memory location. (Changes in one, will affect the other.) • Java, CLOS, Object Pascal • If used, languages usually provide a means for true copy
Copies and Clones • How to copy a value that references other objects? • Shallow copy • assignment of references • Deep copy • Overload assignment • Write copy constructor • Overload clone method
Copy constructors • Define what you want to copy • Argument is a reference parameter of same type public class MyClass { public: MyClass(constMyClass source) { ….}; • Considered GOOD practice to ALWAYS include a copy constructor
Cloning in Java • Implement Cloneable interface • Override method clone class PlayingCard implements Cloneable { : public Object clone () throws CloneNotSupportedException { Object newCard = super.clone); return newCard; } } • Default behavior creates a shallow copy. Add additional code to create a deep copy
Equality • Identity – are two objects the same entity • Equality – two objects that contain the same values char * a = “abc”; char * b = “abc”; If (a==b) // T/F?
Equality Testing • Override == • Override or write equals method • There is an equals method in Java’s Object class
Paradoxes of equality testing • Equality can be done at various levels • Let p1 and p2 be parent classes and c1 and c2 be child classes Is p1.equals(c1) same as c1.equals(p1) ? If p1.equals(c1) and p1.equals(c2), does c1.equals(c2)? If p1.equals(c1) and c1.equals(p2), does p1.equals(p2)? • HMMM?
Study questions • Pg. 286: 2-5, 9