I have class A defined in a library:
public final class A {blah}
And then I want to hack it somewhere using a new class B to substitute it:
A a = new A();
A a = (A) new B();
B has all the methods and members in A, so it's actually safe to do this, and I can do it in C++. But for Java, this casting will result in an exception. So is there any workaround or hacking to do this in Java? In addition, is there a way to hack without touching the VM?
Though
B
has all the methods and property doseA
have, you can not cast object ofB
to a reference ofA
. That means -is invalid unless
B
extendsA
.And here
A
is a final class so you can not extendsA
byB
. You have to makeA
non-final and B has to extends A. Then the above casting will be valid.