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 8521165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:44:34+00:00 2026-06-11T06:44:34+00:00

Suppose I have an interface like so public interface Foo<Ret, Arg> { public Ret

  • 0

Suppose I have an interface like so

public interface Foo<Ret, Arg> {
    public Ret doSomething(Arg arg);
}

And a concrete implementing class that only has package visibility.

class FooImpl<Ret, Arg1, Arg2> implements Foo<Ret, Arg>, Bar<Ret, Arg1, Arg2> {
    // This class also implements something else making use of Arg2, so you cannot safely remove it
    // But nothing relating to Arg2 is needed to create a FooImpl
}

(An example of how this might happen is the implementation of Foo and Bar in FooImpl forwards methods declared by the two interfaces to one varargs method)

Now, suppose there is a static method somewhere in the same package that returns a FooImpl as a Foo. One would think that you could use return new FooImpl<Ret, Arg1, ?>(...), when in fact you can’t (you have to use a concrete dummy type as Arg2, i.e.. return new FooImpl<Ret, Arg1, Object>(...), say).

Any idea why this is, especially as the fact that the Foo interface and the package visibility effectively hides the FooImpl from whatever is using the static method? Is it because of the fact that one could still use reflection to some extent in order to get to the Bar parts of FooImpl, where a concrete type is needed?

  • 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-11T06:44:35+00:00Added an answer on June 11, 2026 at 6:44 am

    The Java compiler tries hard not to be smart. There are many situations when it is obvious that it doesn’t need some information to produce correct code but it still complains. When Java was invented, the world had seen C with it’s sloppy attitude to try to compile everything, no matter how dangerous or stupid. The goal for javac was to make sure people can’t shoot themselves into the foot easily.

    Therefore, it assumes that there is a reason that you mention Arg2 – if it wasn’t necessary for something, you surely wouldn’t have put it into the code.

    Solutions:

    1. Assign a type when you implement Bar: class FooImpl<Ret, Arg1> implements Foo<Ret, Arg>, Bar<Ret, Arg1, Object>
    2. Create a BetterFooImpl<Ret, Arg1> extends FooImpl<Ret, Arg1, Object>. That hides the third type argument from consumers of the API.
    3. Try to get rid of the third parameter of Bar
    4. Use generics carefully. It might be surprising, but there are situations where generics simply don’t work well. This is usually because of subtle implementation details which cause an explosion of @SuppressWarning annotations. When this happens, I ask on SO and when I can’t find a solution that I can understand, I remove the generics. It’s more important to write maintainable code than finding clever ways through the generics maze.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a class that implements an interface: public class A implements IB
Suppose I have interfaces as follows: public interface UnaryFunction<Ret, Arg> { public Ret invoke(Arg
Suppose you have an interface like this: public interface IDoSomething<out T> { T DoSomething(object
Suppose I have a few definitions like so: public interface ICategory { int ID
Suppose I have these interfaces: public interface I1 { void foo(); } public interface
Suppose I have an empty interface class IBaseInterface which is used only to label
Suppose a construct like this: class Interface { public: template <typename T> virtual void
Suppose I have an interface for a service: public interface IFooService { void DoSomething();
Suppose I have this class hierarchy: class A { public: virtual void foo(Base *b)
Suppose I have this: @Transactional(rollbackFor = NotificationException.class) public interface PersonManagerService { public void addPerson(Person

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.