2

I created interface:

public interface SubApp {
           public String talk(Result<? extends Object> result);
}

And this implementation:

class A implements SubApp{
   @Override
   public String talk(Result<String> result) {...}
}

My IDE gives following error: Method does not override method from its superclass.

How do I define method in interface to make method public String talk(Result<String> result) override method of interface?

4 Answers 4

3

The issue with your approach is that your implementation is more specific than the interface definition, and thus your method won't accept the breadth of input your interface will, and that's not allowed. (You can refine output types to be more specific, because an output type can be downward cast to the less specific type, but only the opposite is true for inputs).

Something like this should work:

public interface SubApp<T extends Object> {
    public String talk(Result<T> result);
}

class A implements SubApp<String> {
    @Override
    public String talk(Result<String> result) { ... }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try something like this -

public interface SubApp<T> {
  public String talk(Result<T> result);
}

class A implements SubApp<String> {
  @Override
  public String talk(Result<String> result) {...}
}

Comments

1

Try something like

public interface SubApp<T> {
   public String talk(Result<T> result);
}

btw, you never need ? extends Object since everything extends object, you are not liming anything with that expression

1 Comment

you never need ? extends Object -> Not true. It might be needed depending upon whether you want same reference to refer different parameterized type or not.
0

? represents a wild-card but not a generic. Try the following.

public interface SubApp<X extends Object>
{
     public String talk(Result<X> result);
}

Comments

Your Answer

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.