Detailed explanation of inheritance can be found at Inheritance. The whole inheritance concept has a lot to do with the concepts around overriding.
-
In Java, the
toString()method is actually present inside the Object class (which is a superclass to all the classes in Java). You can read more about it here. -
To override this method inside your implementation class, you should have a method with same name i.e
toString()and same return type i.eString.
-
Consider having a method
isVulnerable()inside theFighterclass which states vulnerability of the fighter, returnfalseto make it non-vulnerable by default. -
This can than be overridden by any child class(the class extending
Fighter), according to its requirements. -
Again the overriding concept will come handy.
-
Preparing a spell can only be done by a wizard. So, it makes sense to have this property defined inside the
Wizardclass. -
Create
prepareSpell()method insideWizardclass. -
Remember : Parent class(here
Fighter) has no access to the properties of the child class(for example,Wizard)
- Override the
isVulnerable()method in theWizardclass to make Wizards vulnerable if they haven't prepared a spell.
- Use a conditional statement to return the damage points, taking into account the value of the prepare spell field.
- Use a conditional statement to return the the damage points, taking into account the vulnerability of the target.