I chose this title because I noticed that I did something wrong with the implantation of an abstract class, but Iβm not quite sure yet.
I created the abstract class MoveAble for training purposes and created the Ball class from it. I also created the GetPosition () method only for the moveAble class and used it from the ball class. But when I called GetPosition () on any spherical object, I got the varibale position of the Moveable Abstract object instead.
I suppose this is so, but from my understanding we still cannot use an abstract class, so I need to get the position value of the child class, even if I only implemented this method on the parent class.
Note. I am a beginner Java programmer. probably the best way to do what I did, but that is what I came out with. I would like to hear what you guys think about it, And if you think that everything is crooked, and there is a better way for all this, I will be glad to know it.
Roaming class:
public abstract class MoveAble {
private int[] position = new int[2];
private int[] velocity = { 1, 1 };
public int[] getPosition() {
return position;
}
public abstract void move(int width, int height) ;
Ball Class:
public class Ball extends MoveAble{
private int[] position = new int[2];
private int[] velocity = { 1, 1 };
public Ball(int x_position, int y_position) {
position[0] = x_position;
position[1] = y_position;
}
@Override
public void move(int width, int height) {
if (position[0] > width - 30 || position[0] < 1) {
velocity[0] *= -1;
}
if (position[1] > height - 30 || position[1] < 1) {
velocity[1] *= -1;
}
position[0] += velocity[0];
position[1] += velocity[1];
}
source
share