0

I am trying to use an existing package to create my own app upon it. However I do not know how to call an interface parametrized method.

On the package there is a class that's constructor is

public class App{
protected App(Logic logic) {
 this(logic.configuration().welcomeScreen, logic.configuration().appName, Optional.of(logic));
}
}

And the interface is this:

public interface Logic extends X, Y {
default Configuration configuration() {
 return new AppConfiguration(1000, "Hello world", true);
}
default void initialize() {
 System.out.println("Starting the application.");
}
}

And Configuration goes like this:

public final class Configuration {
public final int tick;
public final String appName;
public final boolean welcomeScreen;
public Configuration(int tick, String appName, boolean welcomeScreen) {
 this.tick = tick;
 this.appName = appName;
 this.welcomeScreen = welcomeScreen;
}
}

How do I call the App with the configurations that aren't default (1000, "Hello world", true)?

The question is similar to this: interface as a method parameter in Java but I can't grasp on the idea of having interface as parameter.

asked Sep 27, 2018 at 20:01

1 Answer 1

1

You provide a class that implements the interface and override those methods; as simple as that. default methods are overridable and your method that takes this interface as parameter can take that new class as parameter (since it implements the interface); and thus methods from the class will be called.

answered Sep 27, 2018 at 20:03
Sign up to request clarification or add additional context in comments.

5 Comments

There comes an error that I cannot override the method. That is since there is no public-notation on the configuration() method. Can I access the App(Logic logic) somehow through the AppConfiguration-method which is public?
@Hjell no idea how that would not be possible. For example: interface Test { default String getIt() { return ""; } } class Impl implements Test { @Override public String getIt() { return ""; } }
My bad, the errror was setting it public. (No idea why it did not compile correctly without it). Now do I just set the example of Impl as parameter?
@Hjell if that helped you could accept the answer
It helped but did not solve my problem. Now when I overrride it doesn't recognize it for some reason. Code is as follows: public class Z implements Logic{ @Override public Configuration configuration(){return new Configuration(10,"Test",false);} public static void main(String[] args){launch(args)}} Still it uses the same values as default configuration().Is there a reason why that doesn't work?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.