I have three classes. I want to execute the CodeBlock2 in the Parent class's execute
method conditionally based off of a flag I would be setting while calling this method through MyChild class.
For the legacy purposes, modifying the signature of the execute
method in Parent class to have an extra boolean argument is not possible.
Q1. In CPP, I have used lambda functions to achieve similar effects; which in my experience, involved a lot of code modifications all across the package wherein equivalent of Parent class was used. I'd refrain from such given the criticality and coverage of my change. Does Java have some similar or easier way do achieve this?
Q2. I think using instance variable with a flag in the Parent class would also be possible, maybe not as elegant. Could someone chime in on this solution as well?
I would really appreciate your opinions. There are some related but not quite similar questions 1, 2.
public abstract class Parent extends GrandParent
{
@override
public void execute(String A)
{
// CodeBlock1
// CodeBlock2 // I wanna put this under if condition based on a flag in MyChild
// CodeBlock3
}
}
public class Child extends Parent
{
@override
protected boolean someMethod()
{
// code
}
}
public class MyChild extends GrandParent
{
@override
public boolean execute(String A)
{
String B = "123";
child.execute(B);
// child.execute(B, true); // what I wanna do
}
private Child child;
}