Java: Calling a method from a wrong class
<p>I am a total newbie in Java and I am trying to write a bouncing ball program. It should create a ball at a pre-determined location on the screen and the ball should bounce it's way off the screen. When I put all the methods in one class, it works perfectly. However, when I try to accomplish the task using two classes, I fail.</p>

<p>Here is the Ball class which basically just creates the new ball:</p>

<pre><code>import acm.graphics.*;
import acm.program.GraphicsProgram;
import java.awt.Color;
import java.util.Random;

public class Ball extends GraphicsProgram{

    private GOval ball;
    private int diam;

    public Ball(int width, int height){
        Random rand = new Random();
        diam = rand.nextInt(15) + rand.nextInt(15);
        ball = new GOval(width, height, diam, diam);
        Color c = new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255));
        ball.setFillColor(c);
        ball.setFilled(true);
        add(ball);
    }

    public Ball(){
        Random rand = new Random();
        diam = rand.nextInt(15) + rand.nextInt(15);
        ball = new GOval(50, 100, diam, diam);
        Color c = new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255));
        ball.setFillColor(c);
        ball.setFilled(true);
        add(ball);
    }

    public int getDiam(){
        return(diam);
    }
}
</code></pre>

<p>And here is the class which should extend the Ball class:</p>

<pre><code>//import acm.graphics.*;
//import acm.program.GraphicsProgram;
import java.awt.Color;
import java.util.Random;

public class BouncingBall extends Ball{

    private final static int START_X = 50;
    private final static int START_Y = 100;

    private final static int GRAVITY = 3;
    private final static double BOUNCE_REDUCE = 0.90;

    private final static int DELAY = 50;

    private final static double X_VEL = 5;

    private double xVel = X_VEL;
    private double yVel = 0.0;

    private Ball ball;
    private int diam;

    public static void main( String[] arg ){
        new BouncingBall().run();
    }

    public void run(){
        setup();
        while(ball.getX() &lt; getWidth()){
            moveBall();
            checkForCollision();
            pause(DELAY);
        }
        remove(ball);
    }

    private void setup(){
        ball = new Ball(START_X, START_Y);
    }

    private void moveBall(){
        yVel += GRAVITY;
        ball.move(xVel, yVel);
    }

    private void checkForCollision(){
        diam = ball.getDiam();
        if (ball.getY() &gt; getHeight() - diam){
            yVel = -yVel * BOUNCE_REDUCE;

            double diff = ball.getY() - (getHeight() - diam);
            ball.move(0, -2 * diff);

            Random rand = new Random();
            Color c = new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255));
            ball.setFillColor(c);
            c = new Color(rand.nextInt(255), rand.nextInt(255), rand.nextInt(255));
            setBackground(c);
        }
    }
}
</code></pre>

<p>What bothers me is the following. I get errors in the BouncingBall class in the line</p>

<pre><code>ball.move(xVel, yVel);
</code></pre>

<p>, line</p>

<pre><code>ball.move(0, -2 * diff);
</code></pre>

<p>and line</p>

<pre><code>ball.setFillColor(c);
</code></pre>

<p>and the error I am getting is "method move in class java.awt.Component cannot be applied to given types;" for the former two occurrences and "cannot find symbol" for the latter one. Both of these are because the move and setFillColor are being looked for in the wrong classes instead of the GObject class which, as far as I get it should provide its methods as inherited to Ball and then - to BouncingBall.</p>

<p>I can't seem to find an explanation as to why aren't the inherited methods being called - I'd be grateful if someone could help me out.</p>

<p>Thanks!</p>
