C11, Implications of Inheritance - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

C11, Implications of Inheritance

Description:

Clones: Java. Class Object provides protected method clone() creating a bitwise copy of the ... Programmer must override clone() to public and implement Cloneable: ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 16
Provided by: bern8
Category:

less

Transcript and Presenter's Notes

Title: C11, Implications of Inheritance


1
C11, Implications of Inheritance
2
Implications
  • OO language must support polymorphic variables
  • Polymorphic variables memory requirements
    unknown at compile-time gt allocated on the heap
  • Heap allocation gt reference semantics for
    assignment and parameter parsing
  • Also equality testing object identity
  • And memory management necessary gt garbage
    collection (GC)

3
The Polymorphic variable
Shape x,y describe()
Circle radius describe()
Square side describe()
Shape form new Circle(10,10,5) form.describe(
) form new Square(15,20,10) form.describe()
4
Memory Layout
  • Stack-based vs. heap-based
  • Stack-based tied to method entry/exit
  • 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.

5
Factorial example
Factorial(3)
  • static public int factorial(int n)
  • int c n-1
  • int r
  • if (cgt0)
  • r nfactorial(c)
  • else
  • r 1
  • return r
  • 0 n1
  • r1
  • c0
  • 0 n2
  • r?
  • c1
  • 0 n3
  • r?
  • 8 c2

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

7
Assignment reference semantics
  • Box x new Box()
  • x.setValue(7)
  • Box y x
  • y.setValue(11)
  • System.out.println(x.getValue())
  • System.out.println(y.getValue())
  • 11
  • 11

x a box y
Simple copy pointer value, i.e. point to same
heap-location!
8
Clones
  • If copy desired, must say so, DIY approach
  • Box y new Box()
  • y.setValue(x.getValue())
  • If common, package into proper method
  • public Box copy()
  • Box b new Box()
  • b.setValue(getValue())
  • return b

9
Clones Java
  • 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.setValue(getValue())
  • return b
  • Use Box y (Box) x.clone() // must cast !!!!

10
Clones caveats
  • Just a shallow copy, sometimes need deep copies

x a box
a shape y
a box x a box
a shape y a box
a shape
SHALLOW DEEP
11
Parameter passing as assignment
  • 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.setValue(11)
  • x.setValue(7) sneaky(x)
  • System.out.println(x.getValue())
  • 11

12
Equality test object identity
  • Easy for primitives
  • 7 (34)
  • a \141
  • 2 2.0
  • For objects implements object identity,
    therefore
  • new Integer(7) ! new Integer(34)
  • If we really want object equality instead of
    object identidy equals method

13
Equality test object equality
  • Supplied by class Object, can be overridden
  • class Circle extends Shape
  • public boolean equals(Object arg)
  • return ((arg instanceof Circle)
  • (radius ((Circle) arg).radius))
  • Careful must be symmetric and transitive
  • Heuristic use for numbers and null, equals in
    all other situations

14
Equality test bug example
  • Suppose
  • class Shape
  • public boolean equals(Object arg)
  • return ((arg instanceof Shape)
  • (x ((Shape) arg).x)
  • (y ((Shape) arg).y))
  • And no equals defined in class Square, then
  • Square s new Square(10,10,5)
  • Circle c new Circle(10,10,5)
  • s.equals(c) // succeeds
  • c.equals(s) // fails

15
Garbage collection (GC)
  • Heap-based memory not automatically recovered on
    method exit (unlike stack)
  • Manual management error-prone (free in C)
  • Forget to free memory leaks
  • Free multiple times
  • Access freed memory
  • Java compromise have automatic garbage
    collection
  • some runtime cost, but not too bad)
Write a Comment
User Comments (0)
About PowerShow.com