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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:57:35+00:00 2026-05-26T14:57:35+00:00

I am using a fluent interface with inheritance. I declared the base class Constructor

  • 0

I am using a fluent interface with inheritance. I declared the base class Constructor protected so you cant create a Foo<Bar> which would result in a ClassCastException on calling add(). But i am having trouble with the static method that returns a new Foo instance.

public class Foo<T extends Foo<T>> // if i change to extends Foo i only get warnings
{
        public static Foo<Foo> createFoo() // <-- error
        {
                return new Foo<Foo>(); // <-- error
        }

        protected Foo() {}

        public T add()
        {
                //...
                return (T)this;
        }
}

public class Bar extends Foo<Bar>
{
        public Bar sub()
        {
                //...
                return this;
        }
}

This is mostly an excercise(personal not homework) in Fluent Interfaces, Domain-specific language and Generics, so please dont ask what i need it for.

Edit: Eclipse error

Bound mismatch: The type Foo is not a valid substitute for the bounded parameter <T extends Foo<T>> of the type Foo<T>
  • 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-26T14:57:35+00:00Added an answer on May 26, 2026 at 2:57 pm

    You essentially have a recursive type declaration.

    Foo<T extends Foo<T>>.

    So let’s say you have a Foo<Foo>. That means T is mapped to Foo. But Foo is not subtype of Foo<T>, which in this case is Foo<Foo>, so what you’re really looking for is Foo<Foo<Foo>>. But wait a minute, the innermost Foo isn’t typed, so I guess it’s Foo<Foo<Foo<Foo>>>…oh forget it!

    To put a more understandable face on it, consider if you had Foo<T extends List<T>>. What could you possibly use for T in a declaration/instantiation of Foo? List<String>? List<List>?

    Edit

    It looks like you found a way to “break” the recursion cycle. You eventually need to get to a reified type. In the same way that you found that ConcreteFoo worked for you, you could similarly for the List example above have some class ConreteListOfItself implements List<ConreteListOfItself> that would break the recursion cycle.

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

Sidebar

Related Questions

I am trying to register the following class using the fluent interface: public class
I am using the fluent interface to create a Zend DB Select object/query. As
I'm trying to using the Subsonic Fluent query interface to create a simple inner
Using Fluent Interface design here if i call something like dog.Train(Running).Train(Eating).Do(Running).Do(Eating); what is the
Is there any Fluent WCF interface out there worth using? Maybe something like these:
Is there any jpa 1.0 fluent api/interface for query building? I'm using openjpa 1.x,
I was trying out parsing XML using Fluent interface as described here . The
I am looking for examples and experience of using fluent interface to define simple
I'm using Fluent NHibernate to map a a class that has a collection of
I have dictionary which I'm mapping using Fluent NHibernate. The dictionary has a complex

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.