Everybody says that we need to make every variable in class private and access to them with getters/setters. I'm trying to understand why we always need to do this? For example, if I make a simple game and create an Object class, which stores x and y coordinates of any object and other objects (e.g. sprites), isn't it better to do:
public class Object
{
public float x, y;
public Object(float x, float y)
{
this.x = x;
this.y = y;
}
}
public class Sprite extends Object
{
public Sprite(float x, float y, /* other stuff, e.g. img src */)
{
//...
}
}
and use it like:
sprite1 = new Sprite(/* ... */);
checkSomething(sprite1.x, sprite1.y);
Isn't it more natural and faster? Like if we need to compare objects coordinates thousands times in one frame? We don't need to validate coordinates - every float is okay. So why complicate it with functions like getX() and setX(/* ... */) doing the same thing?