0

I’ve method which I inherit from the base class.
I’ve 5 class that inherit the method and override it but doing the same, Now I need to change all the methods to support additional scenario according to envoriment variable

//specific  implications
@Override
protected boolean prepareDirectory(Configuration configuration, Logger logger) throws Exception

    //logic here as before
    if(env===“global”) {

        //do something 
    } 
}

Of course I can do this if inside all the class which works but there is a better approach to overcome this.
Since all the 5 class should use the same logic inside the new if.

3
  • 1
    if(env===“global”){ is not valid Java. Are you confusing Java and JavaScript? These are two different programming languages.
    – Jesper
    Dec 14, 2017 at 11:12
  • @Jesper - this is only pasudo code, but assume that I getting env
    – 07_05_GuyT
    Dec 14, 2017 at 11:13
  • @Jesper: Presumably a typo, like the quotes. @Override, protected, boolean, throws, and types on the parameters all suggest Java, not JavaScript. Dec 14, 2017 at 11:13

3 Answers 3

2

You can achieve it with an abstract method. Something like

abstract class BaseClass {

    abstract void basePrepare(Configuration configuration, Logger logger);

    protected boolean prepareDirectory(Configuration configuration, Logger logger) throws Exception {
    if (/* condition */) {
        basePrepare(configuration, logger); // implementing class does his stuff
      }
    }
}
3
  • Thanks, can you explain bit more how I combine this class to work on each of my 5 class ?
    – 07_05_GuyT
    Dec 14, 2017 at 12:31
  • in addition all the 5 classes inherit from abstract that Im not sure that I can modify with additional code...
    – 07_05_GuyT
    Dec 14, 2017 at 12:37
  • You would make your base class as abstract with the code snippet I posted. The inherited classes have to implemented the basePrepare method. You don't need to override the prepareDirectory in the child classes anymore since there is a designated abstract method for that to be executed. Add more abstracts method if you need different behaviours. The base logic will be untouched and the child classes don't need to know implementation details. Dec 14, 2017 at 12:42
1

If you are working with Java 8, you should use Interface with a default method.

Create an Interface and then create one method with default access modifier, it would be the best approach.

1

You can do that by a Base class that does the common logic of your five classes, and, at the same time, gives the possibility to each one of them to do their specific logics :

 abstract class Base {
       public RETURN_TYPE commonLogic(SOME_PARAMS){

       //common logic goes here

        }

  public abstract RETURN_TYPE specificLogic(SOME_PARAMS);

  }

and then inherit this Base class, implement the specific logic :

  class Logic1 extends Base{

    @Override
        public RETURN_TYPE specificLogic(SOME_PARAMS){

        // your specific logic

      }
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.