Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Cannot subclass the final class" error, but the class is not final [closed]

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 the final class Character.

but I checked a million times and I am yet to use final all but ONCE in my project. What gives?

like image 625
Fletcher Avatar asked Dec 14 '18 12:12

Fletcher


People also ask

Can you subclass final class?

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.

Can a final class be a subclass in Java?

A class that is declared final cannot be subclassed. This is particularly useful, for example, when creating an immutable class like the String class.

Which of the following statements about final class are true?

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.

Is final class Cannot be inherited?

If a class is marked as final then no class can inherit any feature from the final class. You cannot extend a final class.


2 Answers

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).

like image 74
Adam Kotwasinski Avatar answered Sep 23 '22 09:09

Adam Kotwasinski


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 {  } 
like image 23
Chris Avatar answered Sep 24 '22 09:09

Chris