I'm having a problem changing an abstract class to an interface. I changed the abstract class (Bounceable) to an interface but I'm not sure what I need to do with the concrete classes (Tennisball, Basketball) that implement the interface. The change to an interface is also affecting the output test program (PolyTest).
Bounceable.java
/**
* This abstract class implements an interface.
*/
public interface Bounceable
{
/**
* This method is the bounce method and returns the bounce string.
* @return Returns the bounce string.
*/
public abstract String bounce();
}
Tennisball.java
/**
* This concrete class that implements from Ball.
* It supplies the ball type, bounce, and play string.
*/
public class Tennisball implements Bounceable
{
/**
* This method implements the super class constructor.
*/
public Tennisball() {
super("Tennisball");
}
/**
* The play method returns the string.
*/
public void play()
{
System.out.println("All set for a match? I love this game!");
}
/**
* The bound method returns the bounce string.
*/
public String bounce()
{
String bounce = "Boing!";
return bounce;
}
}
Basketball.java
/**
* This concrete class that implements from Ball.
* It supplies the ball type, bounce, and play string.
*/
public class Basketball implements Bounceable
{
/**
* This method implements the super class constructor.
*/
public Basketball() {
super("Basketball");
}
/**
* The play method returns the string.
*/
public void play()
{
System.out.println("Basketball begins with a tip-off!");
}
/**
* The bound method returns the bounce string.
*/
public String bounce()
{
String bounce = "Dribble...";
return bounce;
}
}
Polytest.java
/**
* This class is used to test Ball and its subclasses
*/
public class PolyTest
{
/**
* This method is used to test Ball and its subclasses
*
* @param args command-line arguments (not used by this application)
*/
public static void main(String[] args)
{
//==========================
// Array of Ball instances
//==========================
Ball[] ball = { new Baseball(), new Basketball(), new Bowlingball(),
new Tennisball() };
//=============================
// Display a starting message
//=============================
System.out.println("For each type of ball...\n");
//=====================================
// Loop to process each ball instance
//=====================================
for (int i = 0; i < ball.length; ++i)
{
//==========================
// Display the ball's type
//==========================
System.out.println("Ball #" + (i + 1) + " is a " + ball[i].getType());
//=======
// Play
//=======
System.out.print("Playing: ");
ball[i].play();
//==========================================
// Call the bounce method (if appropriate)
//==========================================
if (ball[i] instanceof Bounceable)
{
System.out.println("Bouncing " + ((Bounceable) ball[i]).bounce());
}
else
{
System.out.println("You can't bounce this ball!");
}
System.out.println("");
}
//======================
// We are all finished
//======================
System.out.println("\nEnd of program.");
}
}