That constructor of Car
class initialize those values so if you create a Car
it will have by default those values.
Solution 1: Create another constructor
public class Car extends MoveableObject{
private int height;
private int width;
public Car(){
super.setX(200);
super.setY(2);
}
public Car(int x, int y){
super.setX(x);
super.setY(y);
}
}
If you use the second constructor you can define the values you want for that instance of Car
.
Car c = new Car(100, 3);
This really does:
public Car(int x, int y){
super(); //Call the creation of the superclass
super.setX(x); //Modify superclass X and Y values.
super.setY(y);
}
Solution 2: Use getters and setter methods (provide they are accesible and they are in Class or Superclasses)
Once you create a Car
you can change the x
and y
values before adding them to the list.
Car c = new Car();
c.setX(100);
c.setY(3);