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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:26:47+00:00 2026-05-24T11:26:47+00:00

I’ve got a generic class with subclass. public class GenericClass<C> { private C value;

  • 0

I’ve got a generic class with subclass.

public class GenericClass<C>
{
    private C value;

    public C getValue() { return value; }

    public void setValue(C value) { this.value = value; }
}

In my code I use subclasses without knowing what subclasses but I know this two subclasses are same type :

GenericClass<?> obj1 = ... // has a value here
GenericClass<?> obj2 = ... // has a value here

// I know that obj1 is the same type of obj2 but I don't know this type.
obj1.setValue(obj2.getValue());

The last line produce a compilation error which is :

The method setValue(capture#11-of ?) in the type GenericClass is
not applicable for the arguments (capture#12-of ?)

How can I do that (something like casting a wildcard…) ?

  • 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-24T11:26:47+00:00Added an answer on May 24, 2026 at 11:26 am

    Since GenericClass<?> holds no information of the actual enclosed type, the compiler has no way to ensure that two such classes are compatible. You should use a concrete named generic type parameter. If you don’t know the concrete type, you may still be able to enforce the sameness of the two type parameters by e.g. enclosing the above code in a generic method:

    public <T> void someMethod() {
      GenericClass<T> obj1 = ... // has a value here
      GenericClass<T> obj2 = ... // has a value here
    
      obj1.setValue(obj2.getValue());
    }
    

    If this is not possible, you may as a last resort try explicit casts, or using raw types. This will trade the compilation error to unchecked cast warning(s).

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

Sidebar

Related Questions

I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I have some data like this: 1 2 3 4 5 9 2 6
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I have just tried to save a simple *.rtf file with some websites 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.