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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:31:20+00:00 2026-05-14T05:31:20+00:00

I saw some code on an unrelated question but it got me curious as

  • 0

I saw some code on an unrelated question but it got me curious as I never saw such construct with Java Generics. What would be the use of creating a generic class that can take as type argument itself or descendants of itself. Here is example :

abstract class A<E extends A<E>> {
    abstract void foo(E x);
}

the first thing that came to mind would be a list that takes a list as parameter. Using this code feels strange, how do you declare a variable of type A ? Recursive declaration !?

Does this even work ? If so did any of you see that in code ? How was it used ?


EDIT

Indeed it turns out my question is the same as this one only phrased differently, but the answers for that question will answer mine as well.

Also thanks for the reference to the Curiously Recurring Template Pattern which gives some historical background and further explanations on the subject.

This old blog entry probably gives the best all around explanation I found for us Java guys.

Now hard to choose a right answer here for they are all helpful, so I will go for the one that ended up yielding the most reading material (referenced above)

  • 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-14T05:31:21+00:00Added an answer on May 14, 2026 at 5:31 am

    Generics aren’t just for containers like lists. This sort of “extends itself” type parameter is used to let the superclass refer to the subclass in places like method parameters and return types, even though no actual specific subclass is available when the superclass is compiled. It’s analogous to the curiously recurring template pattern in C++.

    A subclass of your example would be declared as

    class Foo extends A<Foo>
    

    and the inherited foo() method becomes

    void foo(Foo x)
    

    See how A defined a method that takes a Foo parameter even though it doesn’t actually know about Foo?

    Yes, this sort of thing is unusual, but not unheard of: the built-in Enum class uses a similar trick.

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

Sidebar

Related Questions

I saw some code like the following in a JSP <c:if test=<%=request.isUserInRole(RoleEnum.USER.getCode())%>> <li>user</li> </c:if>
const static int foo = 42; I saw this in some code here on
Here's some code I saw once. Can you see what's wrong with it? [updated]
Looking at learning some C since i saw in another SO question that is
I saw some code as below during a peer-code-review session: char *s = new
I saw some code, html form code which looks like this: <form name=form1 method=post
I saw some code that called methods on scalars (numbers), something like: print 42->is_odd
I saw some code like string password = 11111111; byte[] salt = Encoding.ASCII.GetBytes(22222222); Rfc2898DeriveBytes
I saw some code like this: try { db.store(mydata); } finally { db.cleanup(); }
I saw some code written by another developer that looks something like this: var

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.