Reference subclass method in superclass method

I have a Saw class that extends the Enemy class.

class Enemy {
  int hp, atk, n;
  float spd;
  PVector pos = new PVector(random(width), random(height));
  Enemy(int hp_, float spd_, int atk_, int n_) {
    hp = hp_;
    spd = spd_;
    atk = atk_;
    n = n_;
  }
  
  void update() {
    PVector target = new PVector(vac.x, vac.y);
    target.sub(pos);
    target.setMag(spd);
    pos.add(target);
    subUpdate();
  }
  
  void display() {
    image(enemy[n], pos.x, pos.y);
  }
}

class Saw extends Enemy {
  Saw() {
    super(5, 4.0, 10, 0);
  }
  
  void subUpdate() {
    
  }
}

In update() I want to call the subUpdate() method from the Saw class. Is this possible? I really can’t find any solution.

Hi @nanto2016,

No! But you can call super.update() on your subclass.
So just call subUpdate on your Saw object (would rename it to update though) and inside call super.update(). That is imo what you want.

You need to think on it a bit different…

Cheers
— mnse

1 Like