Java abstract / extends release

I am currently developing a character generation engine for a Java-based text game, but I ran into a problem and can't see where I might have made a mistake. I have a Symbol class, which is abstract, and then another NPCharacter class, which is implied at the core of this.

public abstract class Character { public abstract void generateStats(); } public class NPCharacter extends Character { public void generateStats() { } } 

Eclipse says that "the NPCharacter type cannot subclass a class character." Can anyone see the error here?

Thanks in advance.

+7
source share
6 answers

The compiler confuses your Character with java.lang.Character . Just use the package and make sure you import Character .

 package com.foo; public abstract class Character { public abstract void generateStats(); } 

and

 import com.foo.Character; public class NPCharacter extends Character { public void generateStats() { } } 
+8
source

The problem is that Character is a class in the java.lang , and you cannot extend it because it is final. You should rename your Character class and / or check your import statements: you have a name clash.

+2
source

The compiler says that you cannot extend the java.lang.Character class, which is a final class that cannot be subclassed. (Classes in the java.lang implicitly imported into all Java source files.)

Although the NPCharacter class NPCharacter intended to extend the abstract class Character , the compiler cannot distinguish between the two.

You will need to use the fully qualified class name of the Abstract class Character , or if this is not possible (due to the fact that it does not belong to any package), you will have no choice but to rename the Character abstract class.

But even better, rename the abstract Character tag to something that does not contradict the class name in the java.lang , as this can cause confusion for people reading your code.

+2
source

Character is the standard class in the java.lang , which is automatically imported in each class. Choose a different name for your custom class.

+1
source

Please check the import. It seems that NPCharacter is trying to extend another class called Character (perhaps one from the standard library, which really can be final).

It is preferable to refer to your character class by its full name, i.e. {package name} .Character

0
source

Your code looks fine. Are you sure that your NPCharacter class is really trying to extend the correct character class, and not another (from another package), for example, for example. java.lang.Character?

0
source

All Articles