I have requirement to extend a .class file in my project and need to override a simple method. Consider i have class A which is in some .jar package.Now i want to override test() method of class A.I did it using creating subclass B of A and override it. Now in my application package (it is .jar) Object is created for class A. This object invoke the method of class A. But i want to make a call to class B method.
My idea is to proxy the object creation in whole application. Whenever the obj of class A is created at that time i want to some configuration to create object of class B & handed over to class A initiate.
Some one please help me to implement this type of mechanism.
You can block or control class instantiation only if the constructor is private.
If you can't overwrite the original base class and you want to proxy it, you need a factory and advise programmers to use the factory instead of calling directly the constructor of the base class.
In the factory code you can return a subclass that operates as a proxy of your base class.