Java Beginner
Java Beginner

Reputation: 31

Having trouble extending class (Dont understand whats wrong and dont understand other complex explanations on this site.)

Basically the line super(int health, int strength, int speed, int type); keeps giving me an error that says...

.class expected.

Here is the code:

public class Pokemon {
    private int health;
    private int strength;
    private int speed;
    private int type;

    public Pokemon(int health, int strength, int speed, int type) {
      assert health >= 0;
      assert health <= 300;
      assert strength >= 1;
      assert strength <= 3;
      assert speed >= 1;
      assert speed <= 300;
      assert type >= 1;
      assert type <= 3;

      this.health = health;
      this.strength = strength;
      this.speed = speed;
      this.type = type; 
    }
}

public class PokemonTester extends Pokemon {
    PokemonTester() {
       super(int health, int strength, int speed, int type);
    }
    {
       Pokemon charizard = new Pokemon(100,2,50,1);
       Pokemon blastoise = new Pokemon(150,2,150,1);
       Pokemon venusaur = new Pokemon(300,2,100,1);
    }
}

Upvotes: 2

Views: 59

Answers (3)

Antoniossss
Antoniossss

Reputation: 32535

This is wrong

super(int health, int strength, int speed, int type);

U should pass values here not to "declare them" so for example

super(200,50,50,1);

would compile just fine. super here means that you are invoking constructor of superclass (class that your current class is extending from) In your case this would be a call to a constructor of Pokemon class

Normally you would like to pass some arguments from current constructor to a underlying, nondefault (with some arguments) constructor of superclass

Upvotes: 3

Marv
Marv

Reputation: 3557

When calling the constructor of the super class, you'll have to actually pass values to it. This can be done by including the parameters from your the super class in your constructor:

PokemonTester(int health, int strength, int speed, int type) {
   super(health, strength, speed, type);
}

Which is probably what you intended to do.

Upvotes: 3

ByeBye
ByeBye

Reputation: 6946

Remove type of parameters and add parameters to class constructor.

PokemonTester(int health, int strength, int speed, int type) {
   super(health, strength, speed, type);
}

Upvotes: 3

Related Questions