Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

413
Views
Why do I get incompatible types when I am giving ? extends in both class and return

In one of my classes I have...

public Class<? extends Map<K, V>> getObjectType() {
    return (Class<? extends Map<K, V>>) Map.class;
}

What is wrong with this code

I keep getting

error: incompatible types: Class <Map> cannot be converted to Class<extends Map<K,V>>
    return (Class<? extends Map<K, V>>) Map.class;

Can someone point me in the right direction

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

K and V aren't actual types and would be erased by type erasure anyway. Just use Map. And there is no need to cast. You only cast to a more specific type and Map.class is an instance of Class<? extends Map>.

public Class<? extends Map> getObjectType() {
    return Map.class;
}

other examples using compatible types:

public Class<? extends Map> getObject1Type() {
    return HashMap.class;
}

public Class<? extends Map> getObject2Type() {
    return LinkedHashMap.class;
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!