Java seems to allow creating new methods for a class but I'm not sure how such methods could be used as calling it from the object results in an error. Here is the code example:-
class Dog {
int x = 5;
int y = 10;
int getX() {return x;}
}
class Main {
public static void main(String[] args) {
Dog dog = new Dog(){
int getX() {return super.x;}
int getY() {return super.y;}
};
dog.getX(); // Works
dog.getY(); // Returns -> cannot find symbol: method getY()
}
}
Is there any circumstance where such methods could be used or should they be avoided?