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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:51:53+00:00 2026-05-29T21:51:53+00:00

import java.util.*; // Let’s define a self-referential type: class SelfReferential<T extends SelfReferential<T>> {} //A

  • 0
import java.util.*;

// Let's define a self-referential type:
class SelfReferential<T extends SelfReferential<T>> {}

//A complete (i.e. not parameterized) subtype of SelfReferential:
class SubclassA extends SelfReferential<SubclassA> {}

//A partial (i.e. parameterized) subtype of SelfReferential:
class SubclassB<T extends SubclassB<T>> extends SelfReferential<T> {}

//Two complete subtypes of SubclassB
class SubclassB1 extends SubclassB<SubclassB1> {}    
class SubclassB2 extends SubclassB<SubclassB2> {}

//Now let's define a generic type over SelfReferential:
class Generic<T extends SelfReferential<T>> {}

//No problem creating a subtype for A, B1 or B2
class GenericA extends Generic<SubclassA> {}   
class GenericB1 extends Generic<SubclassB1> {}    
class GenericB2 extends Generic<SubclassB2> {}

//We can also defined a parameterize type for specific types extending SubclassB
class GenericB<T extends SubclassB<T>> extends Generic<T> {}

//However, it does not seem possible to define a non-parameterized subtype of Generic of ANY subtype of SublassB
//My goal is to provide a type alias for GenericB<? extends SubclassB<?>> to avoid 
//having to mention it everywhere in the code. This is like providing an alias for ArrayList<String> using 
class ArrayListOfString extends ArrayList<String> {}

//Unsucessful attempts:
//class GenericAnyB extends Generic<SubclassB> {} //ERROR: bound mismatch
//class GenericAnyB extends Generic<SubclassB<?>> {} //ERROR: bound mismatch
//class GenericAnyB extends Generic<? extends SubclassB<?>> {} //ERROR: invalid syntax: a supertype cannot specify any wildcard
//class GenericAnyB extends Generic<SubclassB<? extends SubclassB>> {} //ERROR: bound mismatch
//class GenericAnyB extends Generic<SubclassB<SubclassB<SubclassB<SubclassB<SubclassB<SubclassB>>>>>> {} // well...
//class GenericAnyB extends <T extends SubclassB<T>> Generic<T> {} //ERROR: this syntax is illegal

Bottom line, I can’t specify the “reference cycle” in the extends clause.

Question: Is this a Java language limitation?

  • 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-29T21:51:54+00:00Added an answer on May 29, 2026 at 9:51 pm

    You’re right that this isn’t possible, just like declaring a variable with a self referencing type is impossible without wildcards or raw types. You wouldn’t be able to directly instantiate a SubclassB for the same reason that you can’t use it as a bound without a self-referencing type parameter.

    See this post for more discussion on this limitation: Self bound generic type with fluent interface and inheritance

    The bottom line is that GenericAnyB would need to be generic itself to use SubclassB as a bound:

    class GenericAnyB<T extends SubclassB<T>> extends Generic<T> { }
    

    Which just adds an extra step in the hierarchy before anything is usable:

    class GenericB1 extends GenericAnyB<SubclassB1> { }   
    class GenericB2 extends GenericAnyB<SubclassB2> { }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple code below: import java.util.ArrayList; public class BoidList extends ArrayList {
import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class LoginServlet extends HttpServlet {
import java.util.*; import java.io.*; import java.util.regex.*; class ZiggyTest2 extends Thread{ String sa; public ZiggyTest2(String
import java.io.*; import java.util.Scanner; import java.util.StringTokenizer; public class Filereader { public static void main(String[]
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateDemo { public static void main(String[]
Take the following generics example import java.util.List; import java.util.ArrayList; public class GenericsTest { private
I'm confused by the following code: import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class
Given the following program: import java.io.*; import java.util.*; public class GCTest { public static
The following is my Class code import java.net.*; import java.util.*; import java.sql.*; import org.apache.log4j.*;
Let's look at the following code snippet in Java. package demo; import java.util.Calendar; final

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.