I am currently in the process of developing a character generation mechanism for a Java based text game but I've ran into a issue and cannot see where I may have gone wrong. I have a "Character" class, which is abstract and then another class, "NPCharacter" which is meant build on top of this.
public abstract class Character {
public abstract void generateStats();
}
public class NPCharacter extends Character {
public void generateStats() {
}
}
Eclipse says that "The type NPCharacter cannot subclass the final class Character". Can anyone see the error here?
Thanks in advance.
In Java, abstract means that the class can still be extended by other classes but that it can never be instantiated (turned into an object).
A class which is declared as abstract is known as an abstract class. It can have abstract and non-abstract methods. It needs to be extended and its method implemented. It cannot be instantiated.
Yes abstract class does extends the object class. And it inherits properties of object class. We should not be confused here that abstract class can't be instantiate. That will happen in sub class of abstract class but in abstract class we can instantiate the object class and can do override the basic implementation.
Yes! But it makes sense only if the abstract subclass adds more functionality (abstract or not).
The compiler is confusing your Character
with java.lang.Character
. Just use a package and make sure you import your 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() {
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With