1

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;
}
GM005
  • 13
  • 5

2 Answers2

1

If you can extract CodeBlock2 into a separate method in the Parent class, the below would work:

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
    }

    protected void codeBlock2() {
        // CodeBlock2
    }
}

public class MyChild extends Parent
{
    @Override
    protected void codeBlock2() {
        if(condition) {
            super.codeBlock2();
        }
    }
}
  • Really appreciate your answer. While this approach does help running `CodeBlock2`conditionally, I'm also looking to run `CodeBlock3` after that. Which means I have to extract all three CodeBlocks out. Given that this would involve changes in 100s of classes and their test files, I'm really hesitant to do that. – GM005 May 17 '20 at 19:06
0

You can not change the method signature of the parent's method from child,

you can define a boolean flag in the parent with access modifier protected and change

in the child and calling the parent method using super.execute(A)

public abstract class Parent extends GrandParent {

   protected boolean enableCodeBlock2;

   @override
   public void execute(String A) { 
       // CodeBlock1
       if (enableCodeBlock2){
           // CodeBlock2  // I wanna put this under if condition based on a flag in MyChild
       }
       // CodeBlock3
   }
}

, And in the child

public class Child extends Parent {

   @override
   public void execute(String A) { 
       enableCodeBlock2 = true;
       super.execute(A);
   }
}
0xh3xa
  • 4,801
  • 2
  • 14
  • 28
  • Thanks for your answer. As of now, I'd be going with this approach because it'd involve changes to only two classes. – GM005 May 17 '20 at 19:08