I'm trying to override a method for a class that is used in an API without extending it. Is there any way to do this without recompiling the source code? I cannot extend the class because its instance is created inside another class and there are also other classes that need to extend it.
Example:
import A,B,C;
public class Tester{
public static void main(String[] args){
A a = new A();
a.getB().method();
//The object I want to handle is A
//A has-a B and C
//C extends B and is used at some point inside B
//how can I override method() in B?
}
}
^ Addressing this problem is all I need
If you want to take a look at the specific problem, I'm using the htmlunit API and I'm trying to override the onAllChildrenAddedToPage() method in DomNode. The DomNode is used by the WebClient class and needs to be cast into HtmlElement at some point inside WebClient and DomNode. I making a DomNode2 (which extends DomNode) after getting its instance from WebClient, but it gave a ClassCastException because DomNode2 cannot be cast into HtmlElement.