-1

So I wanted to make a 'Ball' class that extends a 'Thing' class. I call the super command for variables that are shared (ex. x,y,sizeX) in the constructor. But when I try to use them (specifically in the jump function) it gives me the error 'MeTryingToMakeAGame.Thing.x is not visible'

Please note that the name of my project is 'MeTryingToMakeAGame.'

class Ball extends Thing {

  Ball(int x, int y, int sizeX, int sizeY, int speed, int jumpheight) {
    super(x,y,sizeX,sizeY);

  }

  void jump() {
    x-=jumpHeight;
  }
}

class Thing {
  private int x;
  private int y;
  private int sizeX;
  private int sizeY;

  Thing (int x, int y, int sizeX, int sizeY) {
    this.x = x;
    this.y = y;
    this.sizeX = sizeX;
    this.sizeY = sizeY;
  }
}

2 Answers2

0

Field x is private, so it is visible only in class Thing. To make fields / methods visible for subclasses, use modifier protected.

Tix
  • 610
  • 6
  • 16
0

Because x is private, you cannot access it directly. In order to access it, you can create a getter method for the x field in the Thing class