Step 5

Creating the AdvancedAttack Class


Let’s start by creating a new Java class, call it “AdvancedAttack”.

We’ll be using this class to create an Attack with a few more advanced functions. This attack will have a range of damage it can give instead of just one single damage value. It will also have a chance of scoring a critical hit and doing double damage!

Understanding Inheritance


Go ahead and add the code to the class as shown here. The import on line 3, and the words extends Attack on line 5.

The word extends means that this class inherits from the Attack class, so what does this mean?

Well, we already have an “Attack” class that works, so why would we recode the same functions in this new “Attack” class? That would just be extra time and work we shouldn’t have to do.

When we inherit a class, that class becomes a parent to the class that inherits it. A parent class gives all of its data that was public or protected.

This means that our new “AdvancedAttack” class will now have all of the functions and variables that the “Attack” class had. This saves us a lot of time! However, we still have one error that we need to fix before we can inherit successfully.

Creating AdvancedAttack class variables


Before we fix the error that we have, let’s go ahead and add some new class variables to this class. Add the code as shown to do so.

Here we have two ints, one for both the minimum and maximum damage values possible with this attack.

We also have a float variable. A float is essentially a decimal number. We use this variable to represent our chance of scoring a critical hit, 1 being 100% and 0 being 0%.

Creating the AdvancedAttack constructor


Now let’s work on fixing that error. Remember, because we inherit from the Attack class we get all of its data, including the constructor. This is where our error comes from.

Because we get the constructor of the Attack class, we need our AdvancedAttack class to use it. Basically, to construct an AdvancedAttack we also need to construct a normal Attack.

Copy the code as shown to do this. Here we create a new constructor for this class that takes in information for all of our new class variables, as well as the name of the attack. Notice the new use of the word super.

The keyword super can be seen as a placeholder for the name of the parent. So here what is really happening is “Attack(name, minDamage)”. We create an Attack, fufilling our requirement, and then we continue on with our other code.

The definitely will be a bit hard to understand at first. The easiest way to think about it is that to construct an AdvancedAttack we need to construct a normal Attack. It wouldn’t make much sense to create an AdvancedAttack without also creating an Attack first, since an AdvancedAttack is an Attack.

You might also be wondering why we don’t save the name of the Attack. Actually we do! Remember, we coded this in the constructor of Attack. So when we use the constructor with super, it saves the name!

Overriding the original getDamage function


Next we need to change up how this new AdvancedAttack calculates damage. To do this we need to create a new getDamage() function. Now, because we already have a getDamage() function from the “Attack” class, we need to override it to replace it with a new one.

Whenever we want to override an original function, the new function has to have the exact same name as the original one. We also have to put the code “@Override” above it to specifically say that is what we are doing.

Completing the new getDamage function


Now that we’ve overriden the original “getDamage” function, add the code as shown to complete it.

We first make use of the “Random” import we made earlier to create a random number generator.

Next, we use the number generator to pick a random number within our damage range. We add the +1 because the number generator does not include the last number. So if you wanted to pick a number from 1-10, you’d have to put 1 and 11. (The ints function actually returns a list of numbers. We tell it to just give us one number by using the findFirst() function. We then convert it into an int by using getAsInt())

After we have picked our random damage, we need to see if we got a critical hit and need to double it. We do this with the next if statement.

Inside this if statement, we first pick a random float using the “nextFloat()” function. If the number it picked is less than our “critChance” variable, we got a critical hit! If we did, multiply our damage by two.

Lastly, once we have our final damage value, we return it. Congrats! We now have completed a much more advanced version of our original Attack class. This add a lot more dynamics to all of our battles for sure!