I created 2 classes in this code example: Monster and Zombie. Monster is a superclass and Zombie is a child class of Monster.
This is my code:
public class Main {
public static void main(String[] args) {
Monster monster = new Monster();
monster = (Zombie) monster;
}
}
This is the Monster
class:
public class Monster {
}
And here is the other class called Zombie
that extends Monster
:
public class Zombie extends Monster {
}
When I run the main method, I get the following error:
class Monster cannot be cast to class Zombie (Monster and Zombie are in unnamed module of loader 'app')
I only get the error in runtime. For context: I was following this tutorial.
CodePudding user response:
it means you cannot turn the monster into a Zombie. Since Zombie is an extension of Monster, you would you something like this:
Monster monster = new Zombie();
does it work now?