C 11 Implications of Inheritance Implications n n

  • Slides: 15
Download presentation
C 11, Implications of Inheritance

C 11, Implications of Inheritance

Implications n n n OO language must support polymorphic variables Polymorphic variables: memory requirements

Implications n n n OO language must support polymorphic variables Polymorphic variables: memory requirements unknown at compile-time => allocated on the heap Heap allocation => reference semantics for assignment and parameter parsing Also: equality testing == object identity And: memory management necessary => garbage collection (GC)

The Polymorphic variable Shape x, y describe() Square side describe() Circle radius describe() …

The Polymorphic variable Shape x, y describe() Square side describe() Circle radius describe() … Shape form = new Circle(10, 5); form. describe(); form = new Square(15, 20, 10); form. describe(); …

Memory Layout n n Stack-based vs. heap-based Stack-based tied to method entry/exit: n n

Memory Layout n n Stack-based vs. heap-based Stack-based tied to method entry/exit: n n n Allocate space for all local vars on entry Deallocate on exit Access by numeric offset (activation record) Need to know space requirements at compile-time, not possible for polymorphic variables! Java: all objects allocated on the heap (new operator), local vars point to these objects, pointers have a fixed size Java has no pointers, but internally all object values are represented by pointers.

Factorial example static public int factorial(int n) { int c = n-1; int r;

Factorial example static public int factorial(int n) { int c = n-1; int r; if (c>0) r = n*factorial(c); else r = 1; return r; } Factorial(3) 0 4 8 9 4 5 n: 1 r: 1 c: 0 0 n: 2 r: ? c: 1 0 n: 3 r: ? 8 c: 2 third activation record second activation record first activation record

Alternative approach (e. g. C++) n n n Different approaches are possible C++: want

Alternative approach (e. g. C++) n n n Different approaches are possible C++: want to use stack (efficiency) Assignment: extra fields are sliced off, n n n E. g. a Square is turned into a Shape, Need to distinguish between virtual/non-virtual methods Need destructors, Pointers/references/values Copy-semantics, …

Assignment: reference semantics Box x = new Box(); x. set. Value(7); Box y =

Assignment: reference semantics Box x = new Box(); x. set. Value(7); Box y = x; y. set. Value(11); System. out. println(x. get. Value()); System. out. println(y. get. Value()); … 11 x 11 a box y Simple copy pointer value, i. e. point to same heap-location!

Clones n If copy desired, must say so, DIY approach: Box y = new

Clones n If copy desired, must say so, DIY approach: Box y = new Box(); y. set. Value(x. get. Value()); n If common, package into proper method: public Box copy() { Box b = new Box(); b. set. Value(get. Value()); return b; }

Clones: Java n n Class Object provides protected method clone() creating a bitwise copy

Clones: Java n n Class Object provides protected method clone() creating a bitwise copy of the receiver, plus interface Cloneable Programmer must override clone() to public and implement Cloneable: public class Box implements Cloneable { … public Object clone() { Box b = new Box(); b. set. Value(get. Value()); return b; }} n Use: Box y = (Box) x. clone(); // must cast !!!!

Clones: caveats n Just a shallow copy, sometimes need deep copies x a box

Clones: caveats n Just a shallow copy, sometimes need deep copies x a box y a box x a box a shape y a box a shape SHALLOW DEEP

Parameter passing as assignment n Passing a variable considered similar to assignment, as same

Parameter passing as assignment n Passing a variable considered similar to assignment, as same value accessible through two different names; pass value, loose some control: static void sneaky (Box b) {b. set. Value(11); } … x. set. Value(7); sneaky(x); System. out. println(x. get. Value()); 11

Equality test: object identity n Easy for primitives: n n For objects: == implements

Equality test: object identity n Easy for primitives: n n For objects: == implements object identity, therefore: n n 7 == (3+4) ‘a’ == ‘141’ 2 == 2. 0 new Integer(7) != new Integer(3+4) If we really want object equality instead of object identidy: equals method

Equality test: object equality n Supplied by class Object, can be overridden: class Circle

Equality test: object equality n Supplied by class Object, can be overridden: class Circle extends Shape { … public boolean equals(Object arg) { return ((arg instanceof Circle) && (radius == ((Circle) arg). radius)); }} n n Careful: must be symmetric and transitive Heuristic: use == for numbers and null, equals in all other situations

Equality test: bug example n Suppose: class Shape { … public boolean equals(Object arg)

Equality test: bug example n Suppose: class Shape { … public boolean equals(Object arg) { return ((arg instanceof Shape) && (x == ((Shape) arg). x) && (y == ((Shape) arg). y)) ; }} n And no equals defined in class Square, then: Square s = new Square(10, 5); Circle c = new Circle(10, 5); s. equals(c); // succeeds c. equals(s); // fails

Garbage collection (GC) n n Heap-based memory not automatically recovered on method exit (unlike

Garbage collection (GC) n n Heap-based memory not automatically recovered on method exit (unlike stack) Manual management error-prone (free in C++): n n Forget to free: memory leaks Free multiple times Access freed memory Java compromise: have automatic garbage collection: n some runtime cost, but not too bad)