I would like to override a 3rd party, open-source final class's non-final method.
final class A
{
void put(obj a)
{...}
obj get()
{...}
}
Please tell me how to override the get() and put() methods, yet still retain the other behaviour and functionality of this class.
It's open source: fork it, and make the class non-final.
You won't be able to extend it without making it non-final.
If your class has a defined interface in which put and get methods are defined then You may want to try to proxy the class.
The easiest way is to create something like this:
public interface CommonInterface {
void put(); Object get();
}
final class A implements CommonInterface
{
void put(obj a)
{...}
obj get()
{...}
}
public class ProxyToA implements CommonInterface{
private A a;
void put(Object a){
// your override functionality
}
Object get(){
// same here
}
void otherAStuff(){
a.otherAStuff();
}
}
And then just use CommonInterface and proxy your object. Or you can use JDK proxies, javassist, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With