Question

So I've looked around for an answer to this error but nothing exact has come up. I'm new to Java and Netbeans and everything I've done so far has been on BlueJ. When I extend a class to another class the variables and methods should be inherited but I keep getting a variable not found error. This is the super class:

package Runner2D;
import java.awt.*;
public class Block {
    protected boolean power;
    public int width;
    public int height;
    public int xPos;
    public int yPos;
    public boolean hit;
    public Block( int x, int y ){
        xPos = x;
        yPos = y;
        width = 30;
        height = 30;
        power = false;
    } // end Block
    public Block( ){
        xPos = ( int ) ( Math.random() * 501 );
        yPos = ( int ) ( Math.random() * 501 );
        width = 40;
        height = 40;
    } // end Block
    public void drawSquare( Graphics2D g2 ){
        g2.fillRect( xPos, yPos, width, height );
    } // end 
} // end Block

And this is the subclass:

package runner2d;    
import java.awt.*;    
public class Invincibility extends Block{    
    public Invincibility( int x, int y ){    
        super( x, y );
        power = true;
        hit = false;
    } // end Invinsibility
    public void setHit( boolean b ){
        hit = b;
    } // end setHit
    public void drawSquare( Graphics2D g2 ){
        if ( !hit ) g2.fillRect( xPos, yPos, width, height );
        else xPos = - 40;`enter code here`
    } // end drawSquare
} // end class

The exact error is cannot find symbol. This worked totally fine in BlueJ.

Was it helpful?

Solution

Your package names differ runner2d and Runner2D Either your classes should be under same package or you should import one into another.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top