Since Scanner
is final
you cannot declare a subclass of it. Period.
- Reflection won't work / help.
- Byte engineering tricks won't work / help.
Conceivably, you could modify the Scanner
class to remove the final
modifier. However, the fact that this is class is in one of the reserved packages means that you would need to modify the Java installation to make this happen. (You might also be able to do it by modifying the bootclasspath ... but that amounts to the same thing.)
But to be honest, if is not really necessary to modify Scanner
. Instead, you could simply copy the Scanner
source code (e.g. from the OpenJDK codebase) and use it to create an "unrelated" class in your application's package space that has the extra method. It won't by type-wise related to the real Scanner
class, but that is unlikely to be a concern.
For the record:
This Q&A - https://stackoverflow.com/a/18746676/139985 - provides some possible reasons why Scanner
does not support readChar
. (Noting of course that it is clearly "opinion".)
The most likely reason for making Scanner
final is security. A final
Scanner class means that a Scanner
instance can be safely passed from non-trusted code to trusted code in a sandboxed environment.