Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8657169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:27:21+00:00 2026-06-12T15:27:21+00:00

Given the following interface, public interface Callback<T> { <K, V> T execute(Operation<K, V> operation)

  • 0

Given the following interface,

 public interface Callback<T> {
    <K, V> T execute(Operation<K, V> operation) throws SomeException;
 }

How would I implement the interface in a new anonymous class where operation is of type Operation<String,String>

e.g, this doesn’t compile:

 Callback<Boolean> callback = new Callback<Boolean>() {
        @Override
        public Boolean execute(Operation<String, String> operation) {
              return true;
        }
 };
 executor.execute(callback);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:27:23+00:00Added an answer on June 12, 2026 at 3:27 pm

    the generics on the method are unrelated to the class generic parameters

    you need to repeat them on the method for it to be correct, e.g.

    Callback<Boolean> callback = new Callback<Boolean>() {
            @Override
            public <X,Y> Boolean execute(Operation<X, Y> operation) {
    
            }
    };
    executor.execute(callback);
    

    In other words the interface requires an execute method that works on any Operation parameters.

    If you want a callback that only works on specific parameters you need to make them part of the class signature, e.g.

     public interface Callback<T,K,V> {
        T execute(Operation<K, V> operation) throws SomeException;
     }
    

    that would then let you do

    Callback<Boolean,String,String> callback = new Callback<Boolean,String,String>() {
            @Override
            public Boolean execute(Operation<String, String> operation) {
    
            }
    };
    executor.execute(callback);
    

    I cannot see a route to getting what you want… unless you start using the <? super K,? super V> or <? extends K,? extends V> forms which may restrict you too much.

    Here is what your interface erases to

     public interface Callback<T> {
        T execute(Operation<Object, Object> operation) throws SomeException;
     }
    

    then when you instantiate with T == Boolean we get

     public interface Callback {
        Boolean execute(Operation<Object, Object> operation) throws SomeException;
     }
    

    which cannot be implemented by a

        Boolean execute(Operation<String, String> operation) throws SomeException;
    

    method as the in parameters are narrower. You can widen in parameters and narrow out parameters but you cannot go the other way.

    That explains why you can change the return type (out parameter) from Object to Boolean as anyone expecting an Object will be happy with a Boolean.

    Conversely we cannot widen the return type as that would give a ClassCastException to anyone calling the method and acting on the result.

    The method arguments (in parameters) can only be widened. Now it is somewhat complex for method arguments as Java sees different types as different methods, so you can legally have

    public interface Callback<T> {
      T execute(Object key, Object value);
    }
    
    Callback<Boolean> cb = new Callback<Boolean> {
      @Override
      public Boolean execute(Object k, Object v) { ... }
      // not an @Override
      public Boolean execute(String k, String v) { ... }
    }
    

    because the second method has a different signature. But your Operation<X,Y> class gets erased to just the raw type irrespective of whether it is an Operation<String,String> or Operation<X,Y>

    There is one thing you could do… but it gets messy!

    public interface StringOperation extends Operation<String,String> {}
    

    then you can do

    Callback<Boolean> cb = new Callback<Boolean> {
      @Override
      public <K,V> Boolean execute(Operation<K,V> o) { ... }
      // not an @Override
      public Boolean execute(StringOperation o) { ... }
    }
    

    but keep in mind that the execute(Callback<?>) method will be calling <K,V> Boolean execute(Operation<K,V> o) and not Boolean execute(StringOperation o)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a base class with the following interface: public class Base { public virtual
Given the following code: public interface IExample { ... } public abstract class BaseExample:
Given the following code: public interface Selectable { public void select(); } public class
Given the following types: public interface IMyClass { } public class MyClass : IMyClass
Given the following public class Service<T> : IService<T> { Repository<T> _repository = new Repository<T>();
Given the following generic interface and implementing class: public interface IRepository<T> { // U
Given the following classes and interfaces class A{ @NotNull(groups=Section1.class) private String myString } interface
Given the following interfaces/classes: public interface IRequest<TResponse> { } public interface IHandler<TRequest, TResponse> where
Given the following code : public abstract class Participant { private String fullName; public
Given the following code snippet: type MyIntf = interface ['{C6184693-663E-419F-B2DA-4DA1A0E33417}'] procedure Foo; end; InvisiblePropInterfaces

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.