Lesson 28 of 34 · Inheritance & Interfaces
Boss: Java Polymorphism Battle Arena
Morvath the Shapeshifter blocks the way. Every test you pass deals damage — pass them all to defeat it and claim a loot chest.
At the last pass of the Bytecode Highlands waits Morvath the Shapeshifter — a wolf one moment, a bear the next, a raven when you least expect it. No single weapon works against a foe that keeps changing form, so the clan elders ask you to build an arena simulator in which any creature can fight any other.
Battle plan
This fight uses the whole chapter:
- an abstract class
Creaturethat holds the shared state (name, HP) and declares an abstractattack(); - subclasses that call
super(...)and overrideattack()in their own way; - an interface
SpellCasterwith a default method, implemented by only some creatures; - polymorphism:
battleworks with twoCreaturereferences and never needs to know which kinds it got.
Assignment
Complete the types in the starter code.
Creature (abstract, already stores name and hp):
isAlive()—truewhile HP is above 0.takeDamage(int amount)— lowers HP byamount. Amounts of 0 or less are ignored, and HP never drops below 0.toString()—"<name> (<hp> HP)", e.g."Brakka (50 HP)".abstract int attack()— performs one attack and returns the damage it deals.
SpellCaster (interface): int getMana() plus a default method canCast() that returns true when the caster has at least 10 mana.
Warrior(String name, int hp, int strength) — every attack deals strength damage.
Mage(String name, int hp, int mana) implements SpellCaster — if it can cast, an attack is a fireball: it spends 10 mana and deals 25 damage. Otherwise it hits with its staff for 5 damage.
Shapeshifter(String name, int hp) — starts in "wolf" form; form() returns the current form. An attack deals the damage of the current form — wolf 8, bear 15, raven 3 — and then shifts to the next form: wolf → bear → raven → wolf → …
Main.battle(Creature a, Creature b) — a attacks first, then they take turns. Each attack is defender.takeDamage(attacker.attack()). As soon as one creature is defeated, return the winner's name. A creature that already has 0 HP when the battle starts loses without a fight (if both are at 0 HP, it is a "draw"). If both are still standing after 100 attacks in total, return "draw".
Main.countCasters(List<Creature> creatures) — how many of the creatures are SpellCasters.
battle(new Warrior("Brakka", 60, 14), new Shapeshifter("Morvath", 70)) // "Brakka"
battle(new Mage("Ilsa", 40, 20), new Warrior("Brakka", 70, 9)) // "Brakka" — the mage runs out of mana
Every passing test strikes Morvath. Pass all ten to win the Highlands' final pass.
Premium lesson
Unlock the full Learn Java course
- All 34 lessons, bosses and quizzes — lifetime access
- One payment of $35, no subscription
- Finish within 30 days and claim $10.50 (30%) back
Want more than one course?
The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.
Keep learning
Need a quick reminder? The Java cheat sheet has every topic of Learn Java on one page.
Lesson last updated on .