I'm trying to learn more about inheritance and abstract classes, and in doing so I have stumbled upon a problem. Although I know ( -or- at least think I know) how to 'fix' the problem, I would like to know what is causing it.
The problem arises as follows:
I have three classes; Character (abstract), Monster (abstract) and Goblin (concrete) - with inheritance hierarchy, Character <-- Monster <-- Goblin (parent left, child right). The (abstract) Character class declares two abstract methods fight(Character opponent)
and move(int distance)
. These methods are implemented in the concrete class Goblin (with identical method signatures). When I try to compile the Goblin class, the compiler complains, stating that the Goblin class is not abstract and does not override the abstract method fight(adventure.Character) in the adventure.Character class.
Now to fix this, I can add an import statement (import adventure.Character
Why do I need to do this?
Isn't the Character class imported via inheritance from the Monster class?
Code and compiler output below.
Thanks Character.java Monster.java Goblin.java
$ javac adventure/monsters/Goblin.java
adventure/monsters/Goblin.java:5: adventure.monsters.Goblin is not abstract and does not override abstract method fight(adventure.Character) in adventure.Character
public class Goblin extends Monster