Chapter 11: More on the Implications of Inheritance - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 11: More on the Implications of Inheritance

Description:

Objects are stored on the heap ... local variables (stuff declared in the method) When a method returns, the activation record is popped. Main: ObjA a = new ObjA ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 10
Provided by: markf91
Learn more at: http://www.cs.uni.edu
Category:

less

Transcript and Presenter's Notes

Title: Chapter 11: More on the Implications of Inheritance


1
Session 23
  • Chapter 11 More on the Implications of
    Inheritance

2
Stack-based Memory
public class ObjB int z 30 public int
doMore(int i) z z i return z

public class ObjA int x 100 public
void do (int y, ObjB myB) int loc 6
int t myB.doMore(loc) ...
Main ObjA a new ObjA() ObjB b new
ObjB() a.do(5, b)
  • Objects are stored on the heap
  • When a method is called, an activation record is
    allocated on the stack to hold
  • return address (where to return after execution)
  • parameters
  • local variables (stuff declared in the method)
  • When a method returns, the activation record is
    popped

3
Consider Factorial Example
  • class FacTest
  • static public void main (String args)
  • int f factorial(3) //
  • System.out.println(Factorial of 3 is f)
  • static public int factorial (int n)
  • int c n 1
  • int r
  • if (c gt 0)
  • r n factorial(c) //
  • else
  • r 1
  • return r

4
Assignment of Objects
  • semantics for assignment simply copies the
    pointer into the heap to the object

pubic class BoxTest public static void main
(String args) Box x new Box()
x.setValue ( 7 ) Box y x
y.setValue( 11 ) System.out.println( xs
value x.getValue())
System.out.println(ys value
y.getValue()) // end main
public class Box private int value
public Box() value 0 public void
setValue (int v) value v
public int getValue() return value
5
Cloneable Interface
  • Java has no general mechanism to copy arbitrary
    objects
  • But, the base class Object provides a clone()
    method that creates a bitwise copy
  • The Cloneable interface represents objects that
    can be cloned
  • Several methods in Javas library use clone()

6
Cloneable Box Class
  • public class Box implements Clonable
  • private int value
  • public Box() value 0
  • public void setValue (int v) value v
  • public int getValue() return value
  • public Object clone()
  • Box b new Box()
  • b.setValue ( getValue())
  • return b
  • // end clone

7
Using the Cloneable Box Class
  • pubic class BoxTest
  • public static void main (String args)
  • Box x new Box()
  • x.setValue ( 7 )
  • Box y (Box) x.clone() // assigns y a
    copy of y
  • y.setValue( 11 )
  • System.out.println( xs value
    x.getValue())
  • System.out.println(ys value
    y.getValue())
  • // end main

8
Equality Test
  • tests for pointer equality, so really tests
    for object identity and not equality.
  • equals() is a method inherited from class Object.
    The Java run-time system uses equals() in a
    number of places and expects to be able to test
    any Object for equality with any other Object.

9
Equality Test Example
  • class Circle extends Shape
  • int radius
  • ...
  • int getRadius() return radius
  • ...
  • public boolean equals (Object arg)
  • if (arg instanceof Circle)
  • Circle argc (Circle) arg
  • if (radius argc.getRadius())
  • return true
  • // end if
  • // end if
  • return false
Write a Comment
User Comments (0)
About PowerShow.com