Inheritance Review
A quick review of inheritance, using the Character and Archer classes, before we make a parent class abstract.
Apprentice · Java · OOP · Updated
You recall that inheritance defines an is-a relationship, where one class inherits all properties of the parent class.

The following code is the Character example from Inheritance and Constructors. The Character class defines the variables, getters and methods a character needs.
The Archer class extends the Character class, and adds a few more features of its own.
Character
public class Character
{
protected static final int attackAmount = 10;
private String name;
private int health;
private int level;
private int experience;
public Character(String name, int health, int level, int experience)
{
this.name = name;
this.health = health;
this.level = level;
this.experience = experience;
}
public boolean isDefeated() { return health <= 0; }
public String getName() { return name; }
public int getHealth() { return health; }
public int getLevel() { return level; }
public int getExperience() { return experience; }
public void attack(Character target)
{
if (isDefeated()){ return; }
target.takeDamage(attackAmount);
}
public void takeDamage(int damage)
{
this.health -= damage;
if (isDefeated()) { health = 0; }
}
public void levelUp()
{
if(isDefeated()) { return; }
this.level++;
this.health += 10;
}
public String specialAbility() { return "Standard"; }
}
Archer
The Archer class extends Character. An archer has all the abilities of a character plus the additional attributes and methods defined here.
public class Archer extends Character
{
private int arrows;
public Archer(String name, int health, int level, int experience, int arrows)
{
// the call to the super constructor must be first
super(name, health, level, experience);
this.arrows = arrows;
}
public int getArrows() { return this.arrows; }
public void addArrow()
{
arrows++;
}
}
Do it: finish the wizard
The game needs another kind of character: a wizard. A wizard is a character, so it gets a name, health and a level from Character. It also has something only wizards have: mana, the magic it spends on spells.
Character is given at the bottom of the file, a shorter copy of the class above (a name, health and a level). The Wizard class is started: it extends Character, passes the name, health and level up with super(...), and keeps its own mana.
For example, a new wizard named Merlin, at level 1 with 100 health and 50 mana, who levels up once:
Wizard merlin = new Wizard("Merlin", 100, 1, 50);
merlin.levelUp();
| Level | Health | Mana | |
|---|---|---|---|
| New | 1 | 100 | 50 |
After one levelUp() |
1 + 1 = 2 | 100 + 10 = 110 | 50 + 10 = 60 |
So after one level-up, Merlin is level 2, with 110 health and 60 mana. His specialAbility() returns "Fireball".
Your job: finish the Wizard class with two methods that override Character's.
levelUp(): runCharacter'slevelUp()first, so the level and health still go up, then add10tomana.- A defeated wizard (health
0) doesn't level up, so it gets no mana either: its level and mana stay as they were. specialAbility()returns"Fireball".- Leave
Characteras it is.
Practiced live on the lesson page: write the Java right there and check it.