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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T11:49:13+00:00 2026-06-02T11:49:13+00:00

First off, I want to say there is no use case for this. The

  • 0

First off, I want to say there is no use case for this. The only thing I am trying to do is explore if this is possible.

What I am trying to do is “rebrand” the return signature of a method in the base interface to that of a child interface.

The goal: declare and implement a method once, but vary the return type to match subinterfaces. I have figured out how to achieve this in some cases, but it breaks down in certain situations.

Imagine if I have base interface B and it has a method B doWork(). Also, there is an implementation of B that implements doWork(). Due to the nature of doWork(), this implementation should be the only one that exists.

Now, this is pretty easy to do with Generics. For the above example:

interface B<T extends B> {
    T doWork();
}

class BImpl<T extends B> implements B<T> {
    @Override
    public T doWork() { return something; }
}

And the child interface/impl would look like this maybe:

interface C extends B<C> {
    void somethingCSpecific();
}

class CImpl extends BImpl<C> implements C {
    @Override
    public void somethingCSpecific() {   }
}

Anyone constructing CImpl would see that doWork() returns a C.

C obj = new CImpl().doWork()  // The money shot.  No casting needed.

And here is where it breaks down… Imagine B now looks like this:

public interface B<T extends B> {
    T thisOrThat(T that);
    boolean something();
}

And I want to do this in BImpl:

class BImpl<T extends B> implements B<T> {
    @Override
    public T thisOrThat(T that) {
        if (that.something())
            return that;
        return this;  //  Error!!  _this_ might be a different T than _that_.
    }
    @Override
    public boolean something()  { return whatever; }
}

Note where the error happens.

Obviously, this can’t work without an unsafe and dubious cast. But if I knew that the implementation of this in the above thisOrThat method was the same as the implementation of that, everything would be ok.

So, to my question. Is there a way to restrict this and that to the same type, without knowing that type a priori?

Or maybe is there a different way to go about doing this, but having the same result? Namely only having to declare AND implement thisOrThat() just once, yet have the return type adapt to the subinterface?

Thanks.

  • 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-02T11:49:18+00:00Added an answer on June 2, 2026 at 11:49 am

    Make your class BImpl abstract and add a view method to it which is implemented by the specific classes extending your abstract base class:

    public abstract class BImpl<T extends B<T>> implements B<T> {
       @Override
       public T thisOrThat(T that) {
           if (that.something())
               return that;
           return this.asT();
       }
    
    
        @Override
        public boolean something() {
            // TODO Auto-generated method stub
            return false;
        }
    
        protected abstract T asT();
    }
    

    Every of your classes still needs to implement T asT() then, but this is simple and compiles without warning:

    public class C extends BImpl<C> implements B<C> {
        @Override
        protected C asT() {
            return this;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off i want to say im attempting (and mostly succeeding) to use purely
so first off, I want to say that this question: Bundle framework with application
First off, I want to say that yes, I do know that doing this
First off I want to say that I am not using threads or multiple
First off I use this code to make the navigation bar always stay fixed;
First off, just want to say the Foursquare API is so awesome and tidy!
First off, before I want to get to the question, I want to say
First off let me just say I'm very new to coding so there are
Let's say we have a texture (in this case 8x8 pixels) we want to
First I want to say thanks to everyone that reads this. Stackoverflow is an

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.