The question may be a stupid one to ask but, kindly help me on this. I need to override the behavior of a class, but I will get only the object of it. Following is the code sample.
I want to use the method Util.newChildComponent(String id) itself in all the places.
class ParentComponent{
public ParentComponent(String id){}
protected void someBehavior(){}
}
class ChildComponent extends ParentComponent{
public ChildComponent(String id){
super(id);
}
protected void childsBehavior(){}
}
public class Util {
public static ParentComponent newChildComponent(String id)
{
ParentComponent fc = new ChildComponent(id);
// Initialize the object, and performs some common configuration.
// Performs different operations on the generated child object.
// The child class has many members apart from childsBehavior().
return fc;
}
}
// The above classes belongs to a jar, so I cannot edit the code.
// I need to use Util.newChildComponent(String id)
// But I need to override the behavior as mentioned below.
public void f1()
{
// TODO: How to override childsBehavior() method using the object
// I have it in pc? Is it possible?
// Util.newChildComponent(id) method decorates the ChildComponent
// So I need to use the same object and just override childsBehavior() method only.
ParentComponent pc = Util.newChildComponent("childId");
// I need to achieve the result below
/*
ParentComponent pc = new ChildComponent(id){
@Override
protected void childsBehavior(){
super.someBehavior();
// Do the stuff here.
}
}; */
}
Thanks in advance.