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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:54:41+00:00 2026-06-14T19:54:41+00:00

I wrote some code using generics and I got into the following situation I

  • 0

I wrote some code using generics and I got into the following situation I didn’t manage to understand:

I have the interface IpRange, and the following class:

public class Scope<IpRange<T extends IP>> {
     List<IpRange<T>> rangesList;
     public List<IpRange<T>> getRangesList() {return rangesList;}
}

Now from some test class if i write the following:

Scope<Ipv4> myScope = new Scope<Ipv4>(); 
scope.getRangesList().get(0)

I’m getting object of IpRange type, but if I’m using a raw type and doing this:

Scope myScope = new Scope();
scope.getRangesList().get(0)

I’m getting Object, and I can’t use the ipRange methods unless i explicitly cast it to Range.

If it would have been List<T> i get it, since i used raw type the compiler has no way to know what is the actual type of the list items, but in this case it will be always IpRange type, so why I’m not getting Object?

The thing is that when I’m creating the scope I don’t necessarily know the actual range type. Consider this constructor: public Scope(String rangeStringList); for all I know, the string could be “16.59.60.80” or “fe80::10d9:159:f:fffa%”. But what I do know is that I passed some IpRange object to the compiler and I would expect to be able to use this interface whether this is ipv4 or ipv6. And since the compiler can know for sure that this is ipRange even if I used row type, i wonder why java chose to do it this way

  • 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-14T19:54:42+00:00Added an answer on June 14, 2026 at 7:54 pm

    People have pointed out that all generic type information is stripped when using raw types, and hinted that this is to do with backwards compatibility. I imagine this might not be satisfactory without an explanation, so I’ll try to explain how such a problem might be encountered with code like yours.

    First of all, imagine the code you have written there is part of an old library, and you’re in the process of upgrading the library by adding generics. Perhaps it’s a popular library and lots of people have used the old code.

    Someone may have done something like this using the classes from your library:

    private void someMethod(Scope scope, Object object) {
      scope.getRangesList().add(object);
    }
    

    Now, looking at this we know that Object might not be of the type IpRange, but this is a private method, so let’s assume that type checking is effectively performed by whatever methods call someMethod. This might not be good code, but without generics it does compile and it might work just fine.

    Imagine that the person who wrote this upgraded to the new version of your library for some new features or unrealted bug fixes, along with this they now have access to more type safety with your generic classes. They might not want to use it, though, too much legacy like the extract above code using raw types.

    What you are effectively suggesting is that even though ‘scope’ is a raw type, the List returned from getRangesList() must always be of type List<IpRange<? extends IP>>, so the compiler should notice this.

    If this were the case though, the legacy code above which adds an Object to the list will no longer compile without being edited. This is one way backwards compatibility would be broken without disregarding all available generic type information for raw types.

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

Sidebar

Related Questions

I'm very new to C#. My boss asked me to wrote some code using
I wrote some code which monitors the output of Stopwatch using a tight loop.
I wrote a simple plugin which sets some css code using wp_options. It all
I'm trying to access some C code via Python using ctypes, so I wrote
I currently using android NDK to write some native code in C. I have
I've got some old command-line argument parsing code I wrote 4 years ago for
I'm trying to write some code using pure SQL using ASP.NET MVC. I assume
I'm trying to write some code using posix threads but I'm stuck from the
My company is beginning to write some code using Scala. I've been moved onto
I am using Windows 2003 to write some PHP code. I use XAMPP Portable

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.