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

  • Home
  • SEARCH
  • 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 583873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:50:19+00:00 2026-05-13T14:50:19+00:00

When I have an interface public interface Foo<T> { T someMethod(); } is there

  • 0

When I have an interface

public interface Foo<T> {
    T someMethod();
}

is there any way to assure that when some class implements this interface then generic type is the same implementig class.

For example:

public class Bar implements Foo<Bar>  {
    Bar someMethod() {
        return new Bar();
    }
}
  • 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-05-13T14:50:20+00:00Added an answer on May 13, 2026 at 2:50 pm

    Yes, this can be done (sort of; see below). (In the C++ world, this is called the “Curiously Recurring Template Pattern“, but it applies in Java too):

    public interface Recur<T extends Recur<T>> {
        // ...
    }
    

    (Note the second mention of T. That’s an essential part of the CRTP.)

    Also, this is how java.util.Enum is defined, so that an enum type called Foo must derive from Enum<Foo>, so it’s not an uncommon pattern in Java either.


    I’d like to say that the above is the end of the story, but tangens rightly points out in their revision that it’s not totally robust, and really not all that different from their answer in character.

    There is one difference (that I can think of) between the Recur<T> solution I have, and the Recur<?> solution that tangens has:

    public interface Recur<T extends Recur<T>> {
        T foo();
    }
    
    class A implements Recur<B> {
        @Override
        public B foo() {
            return new B();
        }
    }
    
    class B implements Recur<A> {
        @Override
        public A foo() {
            return new A();
        }
    }
    

    With <T>, the above would not compile; with <?>, it would. But that’s just splitting hairs; it doesn’t change tangens’s central point which is that given an already valid Recur implementation, you can make subsequent implementations use the already-valid type, rather than itself. I still say that’s worth something, but that’s not worth any more of a something than tangens’s answer.

    In closing, go ahead and upvote tangens’s answer too, if you can. (tangens, you should touch your post so I can upvote you too.) They have a very good point, and I’m sorry that I missed it the first time around. Thanks, tangens!

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

Sidebar

Ask A Question

Stats

  • Questions 361k
  • Answers 361k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer http://en.wikipedia.org/wiki/OpenDocument_software lists several PHP tools for working with ODT. Docvert… May 14, 2026 at 2:59 pm
  • Editorial Team
    Editorial Team added an answer Very simple fix!! Sorry to ask a silly question! binding… May 14, 2026 at 2:59 pm
  • Editorial Team
    Editorial Team added an answer Your code seems correct, and compiles for me: Objective Caml… May 14, 2026 at 2:59 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.