GM005
GM005

Reputation: 13

Modifying functionality of a parent class method (through a child class) without changing its signature

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;
}

Upvotes: 1

Views: 2123

Answers (2)

Jeeno Lentin
Jeeno Lentin

Reputation: 23

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();
        }
    }
}

Upvotes: 1

0xh3xa
0xh3xa

Reputation: 4857

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);
   }
}

Upvotes: 0

Related Questions