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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:36:01+00:00 2026-06-14T21:36:01+00:00

Is the following possible somehow? interface Foo<T> { public void bar(T object); } …

  • 0

Is the following possible somehow?

interface Foo<T> {
    public void bar(T object);
}

…

public void callBar(Foo<?> foo) {
    foo.bar("Hello world!");
}

Clearly, this is not type-safe as it is assuming that Foo<?> in this case actually is a Foo<String>.

But rather than the usual “unchecked” warning, this actually gives me the following error: The method bar(capture#1-of ?) in the type Foo is not applicable for the arguments (String)

Usually there’s some casting I can do to turn this exception into the warning I want, but somehow I can’t find one right now…

Any thoughts (other than “don’t do this!”, please)?

EDIT:

It seems like everyone does want to discuss the “don’t do this”, so let me explain the entire problem I’m trying to solve as elegantly as possible and then maybe someone has a cleaner way to do this…

I’m trying to write a flexible eventbus system, where I don’t need to declare a billion interface types for each event.

I want to have a class EventBus that looks like this:

public class EventBus<GroupType, EventType>  {

  ...

  public void addHandler(EventHandler<GroupType, EventType, ?> handler, GroupType group, EventType... events) {
    //                                                      ^ this is the <?> !!!
    // add handler to list of handlers for group and requested event types
  }


  public <Source> void fireEvent(GroupType group, EventType event, Source source) {
    // call all handlers registered for group and event type
  }
}

where the interface EventHandler looks like this:

public interface EventHandler<GroupType, EventType, Source> {
  public void onEvent(GroupType group, EventType event, Source source);
}

That way, I can simply write my event handlers to look like this:

public class Handler implements EventHandler<Groups, Events, TextBox> {
  public void onEvent(Groups group, Events event, TextBox source) {
    // Do something with source immediately
  }
}

where Groups and Events are Enums that describe the possible event types.

Then I register them with

addHandler(myHandler, Groups.EDIT_BOXES, Events.VALUE_CHANGED, Events.CLEARED, ...);

And I can call them with

fireEvent(Groups.EDIT_BOXES, Events.VALUE_CHANGED, myEditField);

In my code, I know that in the group EDIT_BOXES all sources are of type TextBox and I don’t want to type-cast my life away in every handler I write. That’s why I would like to be able to implement the specific interface in the handler, but call it with an unsafe typecast from the EventBus (which I write once and hide forever) rather than having to write all my handlers like this:

public class Handler implements EventHandler<Groups, Events> {
  public void onEvent(Groups group, Events event, Object source) {
    TextBox usableSource = (TextBox) source;
    // Do something with usableSource
  }
}

And if the cast is wrong, the program will and should crash and burn either way. Even if I put an “instanceof” check in the handler, I will need to somehow throw that as an error. I can do that elegantly, but it doesn’t really matter, since any error in this scenario is a bug in the code that needs to be fixed and not some runtime user-error, which I should gracefully inform the user of.

Now, I’ve seen other libraries implement a completely type-safe event system, but often this involves having to declare interfaces for every possible type of event and every possible type of event handler and sometimes even for functions on the eventbus itself, which, if you ask me, is WAY more pain than it’s worth.

If any of you have a clean way to do what I’m trying to achieve, I’d be stoked. But I’m not sure it’s doable any other way.

  • 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-14T21:36:03+00:00Added an answer on June 14, 2026 at 9:36 pm

    Never mind… Just figured it out:

    public void callBar(Foo<?> foo) {
        ((Foo<String>) foo).bar("Hello world!");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to somehow implement the following scheme: public interface ISomething { void
Is it somehow possible, to accomplish the following: x.hpp - this file is included
is this possible somehow? Following scenario: statelesse local EJB with name A singleton startup
Following the example here http://www.java2s.com/Code/JavaAPI/javax.swing/JOptionPaneshowInputDialogComponentpObjectmStringtintmIconiObjectoObjecti.htm is it possible to somehow make the default jcombobox
A window should stay on top of all other windows. Is this somehow possible
I have the following Java interfaces, public interface JavascriptInterface { public MyIterator getItems(); }
is following somehow possible? And if so how?! I know i could pass a
I was wondering if the following is possible. Create a class that accepts an
I am wondering if the following is possible in MVC 3 I basically have
I would like to know if the following is possible in any of the

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.