Here is my code:
package basic; public abstract class Entity {}
package characters; import basic.Entity; public abstract class Character extends Entity {}
package player; public class Player extends Character {}
I am getting the
The type
Player
cannot subclass thefinal class Character
.
but I checked a million times and I am yet to use final
all but ONCE in my project. What gives?
A final class cannot extended to create a subclass. All methods in a final class are implicitly final . Class String is an example of a final class.
A class that is declared final cannot be subclassed. This is particularly useful, for example, when creating an immutable class like the String class.
7. What is true of final class? Explanation: Final class cannot be inherited. This helps when we do not want classes to provide extension to these classes.
If a class is marked as final then no class can inherit any feature from the final class. You cannot extend a final class.
You are extending java.lang.Character
(which does not need an import, as it comes from java.lang
).
Insert import characters.Character
into your Player
code.
Reference: using package members:
For convenience, the Java compiler automatically imports two entire packages for each source file: (1) the java.lang package and (2) the current package (the package for the current file).
Character is a class of java.lang (the wrapper class of "char"). you have to import characters.Character in your Player class
package player; import characters.Character public class Player extends Character { }
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