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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:07:25+00:00 2026-05-26T20:07:25+00:00

I do not understand why the compiler can not see that a cast is

  • 0

I do not understand why the compiler can not see that a cast is safe when the parameterized type is defined as extending a base class. Here are examples of casts that seem to me like they should be unnecessary. Further, when I do include the cast, my IDE (IntelliJ IDEA) warns that the cast is unchecked, as if to suggest that I am doing something wrong. Is there an idiom that avoids these casts and warnings? Why are the casts needed at all, given that the declaration states that the type extends the base class?

class Shape {}

class Polygon extends Shape {}

public class Foo<T extends Shape>
{
  Set<Polygon> polygons;
  // Why must this be cast?
  Set<T> shapes = (Set<T>) new HashSet<Polygon>(); 

  T getFirst()
  {
    // Why must this be cast?
    return (T) polygons.iterator().next();
  }

  Iterable<T> getShapes()
  {
    // Why must this be cast?
    return (Iterable<T>) polygons;
  }
}
  • 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-26T20:07:26+00:00Added an answer on May 26, 2026 at 8:07 pm

    Let’s assume you’ve instantiated your class like this:

    Foo<Circle> circleFoo = new Foo<Circle>( );
    

    Then, Set<Circle> cannot be safely assigned HashSet<Polygon>

    In getFirst: You cannot safely cast Polygon to Circle

    And in getShapes: you cannot safely cast Iterable<Polygon> to Iterable<Circle>.

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

Sidebar

Related Questions

I want to define a type name in a templated class that I can
How universally is the C99 standard supported in today's compilers? I understand that not
I do not understand pointers. Where can I learn more about them?
There is one thing that I do not understand... Imagine you have a text
Before answering this question, understand that I am not asking how to create my
The C# compiler requires that whenever a custom type defines operator == , it
I've tried to Google this issue, and I can't find anything that I see
I understand what it is for - I can see the benefits of being
I believe I understand that during macro expansion, a macro does not have access
I'm not sure I understand it properly: does a 64 bit OS run/compile code

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.