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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:44:37+00:00 2026-05-23T13:44:37+00:00

Is there a semantic difference between these two declaration or is it only syntactic

  • 0

Is there a semantic difference between these two declaration or is it only syntactic sugar?

class C<T extends C> vs class C<T extends C<T>>

Background: I recently answered a question on generics using the C<T extends C> approach and a peer provided a similar answer based on C<T extends C<T>>. At the end, both alternatives provided the same result (in the context of the question asked). I remained curious about the difference between these two constructs.

Is there’s a semantic difference? If so, what are the implications and consequences of each approach?

  • 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-23T13:44:38+00:00Added an answer on May 23, 2026 at 1:44 pm

    Sure – often these “self types” are used to constrain subtypes to return exactly their own type. Consider something like the following:

    public interface Operation {
        // This bit isn't very relevant
        int operate(int a, int b);
    }
    
    public abstract class AbstractOperation<T extends AbstractOperation<T>> {
        // Lets assume we might need to copy operations for some reason
        public T copy() {
            // Some clever logic that you don't want to copy and paste everywhere
        }
    }
    

    Cool – we have a parent class with a useful operator which can be specific to subclasses. For instance, if we create an AddOperation, what can its generic parameters be? Because of the “recursive” generic definition, this can only be AddOperation giving us:

    public class AddOperation extends AbstractOperation<AddOperation> {
        // Methods etc.
    }
    

    And hence the copy() method is guaranteed to return an AddOperation. Now lets imagine we’re silly, or malicious, or creative, or whatever, and try to define this class:

    public class SubtractOperation extends AbstractOperation<AddOperation> {
        // Methods etc.
    
        // Because of the generic parameters, copy() will return an AddOperation
    }
    

    This will be rejected by the compiler because the generic type isn’t within its bounds. This is quite important – it means that in the parent class, even though we don’t know what the concrete type is (and it might even be a class that didn’t exist at compile time), the copy() method will return an instance of that same subclass.

    If you simply went with C<T extends C>, then this weird definition of SubtractOperation would be legal, and you lose the guarantees about what T is in that case – hence the subtract operation can copy itself into an add operation.

    This isn’t so much about protecting your class hierarchy from malicious subclasses, it’s more that it gives the compiler stronger guarantees about the types involved. If you’re calling copy from another class altogther on an arbitrary Operation, one of your formations guarantees that the result will be of the same class, while the other will require casting (and might not be a correct cast, as with the SubtractOperation above).

    Something like this for example:

    // This prelude is just to show that you don't even need to know the specific
    // subclass for the type-safety argument to be relevant
    Set<? extends AbstractOperation> operations = ...;
    for (AbstractOperation<?> op : operations) {
        duplicate(op);
    }
    
    private <T extends AbstractOperation<T>> Collection<T> duplicate(T operation) {
        T opCopy = operation.copy();
        Collection<T> coll = new HashSet<T>();
        coll.add(operation);
        coll.add(opCopy);
    
        // Yeah OK, it's ignored after this, but the point was about type-safety! :)
        return coll; 
    }
    

    The assignment on the first line of duplicate to T wouldn’t be type-safe with the weaker of the two bounds you proposed, so the code wouldn’t compile. Even if you define all of the subclasses sensibly.

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

Sidebar

Related Questions

Could someone please clarify for me the semantic difference between these two: self.foo =
In Java, is there a semantic difference between using Illegal (as in IllegalArgumentException )
In Haskell, lifted type products mean that there's a semantic difference between (a,b,c) and
Is there any semantic difference between writing assertThat(object1, is(equalTo(object2))); and writing assertThat(object1, equalTo(object2))); ?
Is there any semantic difference between ToXXXX conversion methods and AsXXXX conversion methods in
What is the difference between these two innerclass declarations? Also comment on advantages/disadvantages? case
Hi I have a strange situation here: # Is there a semantic difference between
What is the semantic difference between these 3 ways of using ivars and properties
What is the semantic difference between the following two methods: public static bool IsNullOrEmpty(this
Possible Duplicate: What is the difference between using a struct with two fields and

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.