1 / 20

Session 7 More Implications of Inheritance & Chapter 5: Ball World Example

This session explores the implications of inheritance and objects in Java, including memory allocation, assignment of objects, the cloneable interface, equality testing, and object manipulation in the Ball World example.

nikitaj
Download Presentation

Session 7 More Implications of Inheritance & Chapter 5: Ball World Example

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 7More Implications of Inheritance&Chapter 5: Ball World Example

  2. Implications of Inheritance/Polymorphism • At compile-time, the amount of memory for polymorphic variables cannot be determined, so all objects reside in the heap • Because values reside in the heap, reference semantics is used for assignment and parameter passing • Most natural interpretation of equality is identity. Since programmers often require a different meaning two operators are needed • Garbage collection needed since it is hard for a programmer to know if/when an object is no longer referenced

  3. Assignment of Objects public class Box { private int value; public Box() { value = 0; } public void setValue (int v) { value = v; } public int getValue() { return value; } } 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 } What’s the output? Why?

  4. Assignment of Objects Address Heap: single Box object: 4000 Run-time Stack: Main’s Activation Record: x: 4000 y: 4000 • semantics for assignment - simply copies the address of to the object

  5. Try to fix the problem by creating a copy() method.

  6. 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()

  7. Cloneable Box Class public class Box implements Cloneable { 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 }

  8. 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 }

  9. Equality Test • == tests for pointer equality, so == really tests for object identity (i.e., is it the same object) 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.

  10. 3. What’s wrong with each of the following? public class Ball { private Rectangle location; private Color color; public Color color() { return color; } public Rectangle region() { return location; } ... a) public boolean equals( Ball b ) { return this == b; } b) public boolean equals( Ball b ) { return this.equals( b ); } c) public boolean equals( Ball b ) { return ( location == b.region() ) && ( color == b.color () ); }

  11. equals() 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; } }

  12. equals() of Ball ... public boolean equals (Object arg) { if (arg instanceof Ball) { Ball argBall = (Ball) arg; if (color.equals(argBall.color()) && location.equals(argBall.region()) { return true; } // end if } // end if return false; } }

  13. Chapter 5: Ball World Example

  14. Figuring Out the BallWorld • Why use a Rectangle instead of a Point to represent the Ball’s location? • Modify the color of the ball to be green. • Modify the ball to be much larger and to move much faster. • Modify the simulation so that it runs much longer. • Modify the shape of the window to be 100 X 800. • Modify the Ball so the it initial moves in a random direction and with random magnitude. • (Hint: Use the static double Math.random() in the Java package Math. It returns a random in the range [0.0 – 1.0).

  15. The Role of Inheritance in Java Graphics “Don’t call us. We’ll call you.” Without inheritance, we would have to understand many details of how windows work and how they interact with the operating system. • Some of those details are over our heads at this point. We don’t know enough OOP or Java yet. • Even if we could understand them (and we will eventually), we don’t care about them.

  16. The Role of Inheritance in Java Graphics With inheritance, a BallWorld can act as a “regular” Frame when we don’t care about the details and as a special kind of Frame when we do. But there is more to it than that. Not only does BallWorld inherit a lot of individual methods that we use — but many of the methods in Frame call the methods we implement in BallWorld!

  17. Example: Frame manipulation Your BallWorld should be able to handle: • Frame relocation • Frame resizing • Minimizing/Maximizing • Etc…. But wait, _I_ didn’t write any code to handle this…

  18. Example: The show() and paint() Methods But there is more to it than that. Not only does BallWorld inherit a lot of individual methods that we use — but many of the methods in Frame call the methods we implement in BallWorld! Consider how the BallWorld program displays its output: // BallWorld public void paint( Graphics g ) { aBall.paint( g ); aBall.move (); ... counter = counter + 1; if ( counter < 2000 ) repaint(); else System.exit(0); }

  19. Example: The show(), paint(), and repaint() Methods // In BallWorldApplication BallWorld world=new BallWorld(Color.red); world.show(); • show() inherited from Frame • show() calls paint(Graphics g) • of BallWorld • theGraphics object g passed by • show() has the ability to draw a • host of items to the Frame • the Graphics object g is passed • to paint(g) of aBall • Ball’s paint uses the Graphics • object to put a fillOval on the • screen // In BallWorld public void paint( Graphics g ) { aBall.paint( g ); aBall.move (); ... counter = counter + 1; if ( counter < 2000 ) repaint(); else System.exit(0); } // In Ball public void paint( Graphics g ) { g.setColor( color ); g.fillOval( location.x, location.y, location.width, location.height );

  20. “Don’t call us. We’ll call you.” This is a simple example of... multiple objects collaborating to solve a problem It is also an even better example of... writing a program by filling in the details (e.g., paint()) of another program (e.g., Frame) that already does a lot of work The Java AWT is also an example of a framework, a group of classes that work together to provide generic solutions in an application domain, which programmers can extend to provide specific solutions.

More Related