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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:49:04+00:00 2026-06-15T05:49:04+00:00

Got a little puzzle for a true Java Generics specialist… ;) Let’s say I

  • 0

Got a little puzzle for a true Java Generics specialist… 😉

Let’s say I have the following two interfaces:

interface Processor {
    void process(Foo foo);
}

interface Foo {
    Processor getProcessor();
}

and, for example, the following two implementing classes:

static class SomeProcessor implements Processor {
    static final SomeProcessor INSTANCE = new SomeProcessor();

    @Override
    public void process(Foo foo) {
        if (foo instanceof SomeFoo) { // <-- GET RID OF THIS ?
            // process ((SomeFoo) foo)
        }
    }
}

class SomeFoo implements Foo {
    @Override
    public Processor getProcessor() {
        return SomeProcessor.INSTANCE;
    }
}

Is there some way to make the two interfaces generic in such a way that I don’t need the marked instanceof check in the process() function and still have the following construct work elsewhere in my code?

foo.getProcessor().process(foo);

(where, of course, I don’t know what subclass of Foo I’m dealing with)

In other words: I’m looking for a way to define a function in an object such that it can only return another object that processes the type of object that contained the function. Note: I’m not just talking about processing some least common denominator super-class of the object containing the function (above: Foo), but that object’s actual class (above: SomeFoo).

(This is nowhere near as trivial as it may sound unless I’m really being stupid right now…)

  • 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-15T05:49:05+00:00Added an answer on June 15, 2026 at 5:49 am

    This is uglier than I thought. My take:

    interface Processor<F extends Foo<F>> {
        void process(F foo);
    }
    
    interface Foo<F extends Foo<F>> {
        Processor<F> getProcessor();
    }
    
    interface SomeFoo extends Foo<SomeFoo> {
        @Override
        SomeProcessor getProcessor();
    }
    
    interface SomeProcessor extends Processor<SomeFoo> {
        @Override
        void process(SomeFoo foo);
    }
    

    Now, the following will compile:

    <F extends Foo<F>> void process(F foo) {
        foo.getProcessor().process(foo);
    }
    

    but

    void process(Foo<?> foo) {
        foo.getProcessor().process(foo);
    }
    

    doesn’t, because the compiler can not know that actual type of the passed foo is a subtype of its type parameter, as somebody could write:

        class Bar implements Foo<SomeFoo> { ... }
    

    We can work around this by requiring the subtypes of foo to implement a conversion to their type parameter:

    abstract class Foo<F extends Foo<F>> {
        abstract Processor<F> getProcessor();
    
        abstract F getThis();
    }
    
    class SomeFoo extends Foo<SomeFoo> {
        @Override
        SomeFoo getThis() {
            return this;
        }
    
        @Override
        Processor<SomeFoo> getProcessor() {
            return new SomeProcessor();
        }
    }
    

    Now, we can write:

    <F extends Foo<F>> void process(Foo<F> foo) {
        foo.getProcessor().process(foo.getThis());
    }
    

    and invoke this with

    Foo<?> foo = ...;
    process(foo);
    

    To make it easy to use, I recommend moving the helper method into class Foo:

    abstract class Foo<F extends Foo<F>> {
        abstract Processor<F> getProcessor();
    
        abstract F getThis();
    
        void processWith(Processor<F> p) {
            p.process(getThis());
        }
    }
    

    Update: I think newaccts updated answer shows a more elegant solution, as it does not need the recursive type bounds.

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

Sidebar

Related Questions

Got two little problems with this interface. The worse one is that I have
I got a little problem. Sometimes, when I try to call the following code,
I've got a little puzzler here for the bash scripting experts... I have a
I am trying to drag an image view. I have got little bit of
I've got a little text drawing puzzle under Win32. I'm trying to draw some
got a little problem with MBProgressHUD. I have a webview and want to display
I got a little problem here guys. I'm trying to implement the following scenario:
Got little confused with MVVM and user control events. I have a user control
i got a little problem with facebook javascript api. I have app and page
I'm new in Reporting services and got little confused. in the screen shot you

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.