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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:19:03+00:00 2026-06-08T04:19:03+00:00

Saw this post yesterday: How I instantiate? Include code The user could not get

  • 0

Saw this post yesterday: How I instantiate? Include code

The user could not get the constructor type of a generic class, X to match the type of the object passed into the constructor, IA, even though <X extends IA>.

I didnt really like the only answer provided, as it made the whole point of generics useless if you have to change the M constructor type from X to IA<X>. Surely this is why the generic type for M is <X extends IA>??

Is there really no way to use generics (without any suppressed warnings) for this basic example?

public interface IA<X extends IA<X>>{}

public class A<X extends IA<X>> implements IA<X>{}

public class W<X extends IA<X>>{}

public class M<X extends IA<X>> extends W<X>{
    X anx;

    public M(X x){} //Type X here is not compatibile with IA in the test code
}


//Testing code in a different class
public <X extends IA<X>> void check() {

    IA<X> a = new A<X>();
    W<X> s = new M<X>(a); //Doesn't compile because IA<X> is not the same as 'X', even though <X extends IA> 
    W<X> s = new M(a);    //Compiles, but with suppressed warnings

    X a = new A<X>();  //Doesnt compiler (ignoring dupicate 'a' variable)  
    W<X> s = new M<X>(a); compiles
}

edited to include IA everywhere including ‘extends’

  • 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-06-08T04:19:05+00:00Added an answer on June 8, 2026 at 4:19 am

    You have to do something like this:

    //Testing code in a different class
    public <X extends IA<X>> void check() {
        IA<X> a = new A<X>();
        W<subtypeofIA(IA works as well)> s = new M<subtypeofIA(IA works as well)>(a); //Doesn't compile because IA<X> is not the same as 'X', even though <X extends IA> 
        W<X> s = new M(a);    //Compiles, but with suppressed warnings 
    }
    

    Regarding the warnings, I think they are self-explanatory and they can be summarized as: When you have a generically parameterized type whenever you wan to use it, you have to instantiate the generic parameter to a concrete type. Generic parameters have been introduced in order to generalize code but also to enforce type safeness. Using IA means you throw away the type safety you could’ve gotten by saying : IA < ASpecificType > and the compiler draws your attention on this.

    The following code is the closest I could get to your code and at the same time to make some sense:

    interface IA<X extends IA<X>>{}
    
    class A<X extends IA<X>> implements IA<X>{}
    
    class W<X extends IA<X>>{}
    
    class M<X extends IA<X>> extends W<X>{
        X anx;
    
        public M(X x){} //Type X here is not compatibile with IA in the test code 
    }
    
    
    //Testing code in a different class
    public <X extends IA<X>> void check() {
    
        IA<X> a = new A<X>();
        W<X> s = new M<X>(null); //Doesn't compile because IA<X> is not the same as 'X', even though <X extends IA> 
        W<X> ss = new M(a);    //Compiles, but with suppressed warnings
    
        X aa = new A<X>();  //this is completely illegal  
        W<X> sss = new M<X>(aa); //compiles
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I started learning JavaScript last week, I saw this code yesterday and I did
I saw this post which was helpful in copying a .db file I include
Possible Duplicate: Easiest way to get file's contents in C I saw this post
I saw this post: Typos… Just use option strict and explicit please.. during one
I saw this post on Sitepoint quoting a statement by Rasmus Lerdorf which goes
I saw this post Linq int to string , and tried it var personalInfoQuery
So I saw this post which helped answer part of my question however now
I saw in this post a solution that fits exactly my needs https://stackoverflow.com/a/8858815/1462911 .
I look up a lot through this problem while i saw many post abt
I saw this C# using statement in a code example: using StringFormat=System.Drawing.StringFormat; What's that

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.