Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance with abstract classes

 
Josh William
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

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
^
1 error
 
Henry Wong
author
Marshal
Pie
Posts: 21123
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think what is happening is that you named your class, Character. So, if you don't import it, Java is accidentally compiling it against java.lang.Character, which means that you overloaded the fight() method. This, in turn, means that you never implemented the abstract fight() method.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21123
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do I need to do this?

Isn't the Character class imported via inheritance from the Monster class?


You need to import Character, regardless, as it is in a different package. Monster doesn't need to be imported because it is in the same package.

And no. There is no implicit importing "via inheritance".

Henry
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

This is a surprisingly complicated first post!

You have obviously worked out how to inherit correctly. Simply copying the abstract method into Monster doesn't make much difference; you get an error message about not implementing method from Monster. Look closely at the parameters for Monster; they require a Character, and you need an import to get at the Character class.

The real problem is that the Sun compiler doesn't always give good error messages.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . But I forgot about java.lang.Character. Shows how much confusion you can get by naming of things!
 
Josh William
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help guys.

What caused the confusion was the careless naming of my class 'Character'.

So, two things to note from all of this:

1. Subclasses do **not** inherit their corresponding superclass' import statements. If you want to use something outside of the local package, you must import it -- no exceptions.

2. Be careful not to name classes with existing class names from implicitly imported packages; such as java.lang.*


Changing the class name 'Character' to 'Creature' (as well as all references to Character) and importing the Creature class fixed this problem.

Thanks again
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic