0

I have an abstract class that several classes inherit from with attributes health and damageTaken, and within it there's a method called takeDamage(). Here's what it does:

public double takeDamage(int damage) {
   if(health <= 0) {
      return 0;
   }
   
   damageTaken += damage;
   health -= damage;
   
   return health;
}

Since several classes extend the class, I'm trying to get the class Zombie to be instantiated and call the takeDamage() in a Test class. Here's my code for that:

Zombie rob = new Zombie(100, 12, 3.4, 16, true, 500, "Sample Description");
rob.takeDamage(15);

When I try to call takeDamage(), I get a "cannot resolve symbol" error. I don't want to override takeDamage() in every single class this inherits from, instead I want the superclass' methods to be able to be called when instantiating a subclass. Any way to do that?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
JM223
  • 69
  • 6
  • is zombie really an extension of class having `takeDamage` method??:> – Antoniossss Feb 27 '22 at 09:44
  • yep, my class declaration is: `public class Zombie extends Character { //code } ` – JM223 Feb 27 '22 at 09:44
  • And i dont see code the code for the `Character` – Antoniossss Feb 27 '22 at 09:46
  • `Character` is the one with `takeDamage` in it, there's a bunch more stuff in there so I just put in a placeholder for the actual code – JM223 Feb 27 '22 at 09:49
  • Sorry, not seeing it at all. You need to include whole relevant code. – Antoniossss Feb 27 '22 at 09:50
  • The relevant code is the `takeDamage` and Zombie instantiation that I put in the question, I can’t copy it in the comment due to the character limit. – JM223 Feb 27 '22 at 09:54
  • The relevant code is the definition of zombie and whetever is in the class treee. To me, code for `takeDamage` is irrelevant, since I am only interested into its signature, not the body. – Antoniossss Feb 27 '22 at 09:56
  • All zombie has is its constructor that has `super(damageTaken, health)` and a few more attributes in it – JM223 Feb 27 '22 at 10:08

0 Answers0