Assuming three classes, one being a subclass of the other. Each overwrite the parents’ method.
public class BaseClass {
public void doStuff() {
performBaseTasks();
}
}
public class MiddleClass extends BaseClass {
// {BaseClass} Overrides
public void doStuff() {
performMiddleTasks();
super.doStuff();
}
}
public class FinalClass extends MiddleClass {
// {BaseClass} Overrides
public void doStuff() {
performFinalTasks();
super.doStuff();
}
}
When calling new FinalClass().doStuff(), this would lead to a method
invokation order as follows:
performFinalTasks();performMiddleTasks();performBaseTasks();
I want to bring the perfomFinalTasks() between performMiddleTasks() and
performBaseTasks(). How can I do this?
performMiddleTasks();performFinalTasks();performBaseTasks();
One possible way, if you can make the middle class abstract:
You override the proxy method in your subclass:
A not very polymorphic way of method call chaining, but then again the original design is kind of … odd.