More Implications of Inheritance Ball World Example

advertisement
Session 7
More Implications of Inheritance
&
Chapter 5: Ball World Example
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
Assignment of Objects
public class Box {
pubic class BoxTest {
private int value;
public static void main (String [] args) {
public Box() { value = 0; }
Box x = new Box();
public void setValue (int v) {
x.setValue ( 7 );
value = v;
Box y = x;
}
y.setValue( 11 );
public int getValue() {
System.out.println( “x’s value = “ + x.getValue());
System.out.println(“y’s value = “ + y.getValue());
return value;
}
}
} // end main
}
What’s the output?
Why?
Assignment of Objects
• semantics for assignment - simply copies the
address of to the object
Address
Heap:
single Box object:
4000
Run-time Stack:
Main’s Activation Record:
x:
y:
4000
4000
Try to fix the problem by creating
a copy() method.
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()
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
}
}
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
}
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 any other Object.
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 () );
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;
}
}
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;
}
}
Chapter 5: Ball World Example
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).
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.
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!
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…
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);
Example: The show(), paint(), and repaint()
Methods
// In BallWorldApplication
BallWorld world=new BallWorld(Color.red);
world.show();
// 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 );
• show() inherited from Frame
• show() calls paint(Graphics g)
of BallWorld
• the Graphics 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
“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.
Download