Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to inherit a final class modifying bytecode somehow?

Is it possible to inherit a final class using bytecode manipulations?

like image 459
yegor256 Avatar asked Feb 25 '13 12:02

yegor256


People also ask

Can final classes be inherited?

Final class cannot be inherited by any subclass, If we try to inherit a final class, then the compiler throws an error during compilation.

Can a final class be overridden?

No, the Methods that are declared as final cannot be Overridden or hidden.

Which of the following statements about final class are true a final class Cannot be instantiated?

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.


1 Answers

Yes and no.

You can use bytecode manipulation to change a final class to non-final on the fly. This doesn't even break binary compatibility, so there is no risk of class loader / verifier errors.

However, you have to apply the bytecode modifications to the final class itself. You can't do bytecode manipulation on a child class to make it inherit from a final parent class. Or more precisely, if you do that the modified child class will be rejected by the verifier when loaded together with the final parent class.

like image 190
Stephen C Avatar answered Sep 28 '22 11:09

Stephen C