0

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.");
   }
}
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Ezzaral
0

Generally it is a good idea to actually state the problem you are having, rather than leave everyone to guess.

That said, I can tell you that you need to remove the calls to super() in your constructors because you no longer have that super class. You also need to change your array type in your test class to the interface type, not Ball.

Those were the two that jumped out at me with a quick glance over the code. If there are other problems, post them.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.