1 / 9

Session 23

Session 23. Chapter 11: More on the Implications of Inheritance. 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;

nevaeh
Download Presentation

Session 23

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 > 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( “x’s value = “ + x.getValue()); System.out.println(“y’s 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 Java’s 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( “x’s value = “ + x.getValue()); System.out.println(“y’s 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 anyother 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; } }

More Related