I'm currently working on an abstract Superclass (let's call it Enemy) to provide methods for its subclasses (i.e. minor) like setHealth:
However, whenever an actor of another class (i.e. bullet) accesses an object of minor - or any other subclass which also has the field health - , the field of the superclass is referenced and changed, not the one of the subclass object.
I understand that this is the way java works (the compiler will always look in the direct (sub)class of the object for the respective method, and will look on the class above if it doesn't find the method). So to solve this problem, I would have to write the method in the very subclass, which would then overwrite the one in the superclass.
Regarding this, how would one write a super method that references/changes the fields of the respective subclass? How would one write a super method like getX() or setLocation()?
public abstract class Enemy extends Actor { private double health = 1.0; public void setHealth(double damage) { health = health - damage; } }