That's not possible, because you are trying to call the method subclass on super class reference. And that method is not defined in super class itself. The anonymous class is just a subclass of File
there.
However, a workaround is to go through reflection:
file.getClass().getMethod("doStuff").invoke(file);
The getClass()
method will return the runtime type of file
, and then you can get the method for that class using Class#getMethod()
method.
Well, I'm not a big fan of reflection myself. A better way would of course be to create a class by extending the super class, if you are going to do these kinds of stuff. It would be really a pain in the head, working your way out using reflection, for what can be easily done using a simple modification.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…