Search code examples
javamethodsoverridingbuilder

Override Method in Builder


Example class:

public class Example {

    public boolean exampleMethod() {
        return false;
    }

}

So, now I wanted to add a Builder class (of course I have more things in my Example class). In there, I'd like to override the exampleMethod() method.

So I would like to do something like this:

ExampleBuilder builder = new ExampleBuilder();
//override method

Builder:

public class ExampleBuilder {

    //somehow override the exampleMethod with
    //given arguments (I could do it with
    //ExampleBuilder extends Example
    //but I don't think that is right)

}

Thanks for your answers in advance!


Solution

  • You can Override a declaration of method from interface, so instead you can use :

    interface Example {
        public boolean exampleMethod();
    }
    
    class ExampleBuilder implements Example{
    
        @Override
        public boolean exampleMethod() {
          ///
        }
    }