Download presentation
Presentation is loading. Please wait.
Published byΜελαινη Αντωνιάδης Modified over 6 years ago
1
Chapter 11: More on the Implications of Inheritance
Session 23 Chapter 11: More on the Implications of Inheritance
2
Stack-based Memory Objects are stored on the heap
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 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 return false; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.