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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:50:20+00:00 2026-05-31T11:50:20+00:00

Possible Duplicate: Is List<Dog> a subclass of List<Animal>? Why aren’t Java’s generics implicitly polymorphic?

  • 0

Possible Duplicate:
Is List<Dog> a subclass of List<Animal>? Why aren’t Java’s generics implicitly polymorphic?
Java Generics — Assigning a list of subclass to a list of superclass

With raw types you can easily say something like this.

[...] // MyClass is generic with upper bound of Object

MyClass c = new MyClass<Double>();

[...]

But this isn’t allowed

MyClass<Number> c = new MyClass<Double>() 

I don’t understand why this is. My book tells me why the second doesn’t work, because you can’t add a Integer to a MyClass<Double>. What it doesn’t explains is why MyClass<Double> is a subclass of MyClass<Object> (or the equivalent raw type form), since double is subclass of object.

So why is the first allowed if the second form isn’t. Please realize I am new to this.

Edit: Taking it a step further what would happen in the first example if Number was the upper bound?

You can see here the effects of type erasure

class Untitled {
public static void main(String[] args) {

}
public static<T> void c(T t)
{
    t.SubClassMethod();// this won't work because class Object has no such method. But if I change the upperbound to SubClass it will.
            t.toString() // this will because class Object has such a method
}
}

My point is that why should it matter what the Generic is declared as if it ultimately becomes treated as the upper bound anyway?

  • 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-31T11:50:22+00:00Added an answer on May 31, 2026 at 11:50 am

    The first thing to eliminate your confusion is to understand that the Raw Type is not part of the generics system at all. It is in no way equivalent to Something<Object>. Basically, the reason the Raw Type exists at all is just for backward compatibility. When Generics were introduced in Java 5, some existing classes and interfaces were retroactively made generic. In order for old code that didn’t have generic declarations to compile in the Java 5 or later compiler, the Raw Declarations are legal.

    Any comparison between the way the Raw Type behaves and the way the Parameterized type behaves is in a way a bit fundamentally spurious. The intent of the design is not that the Raw Type be considered an ‘alternative’ to declaring a parameter. New code that uses the Raw Type is incorrect code. It’s legal to compile it so that old code still works.

    My point is that why should it matter what the Generic is declared as
    if it ultimately becomes treated as the upper bound anyway?

    Because the entire point of the generics is to prevent ClassCastException. It will be treated as the actual declared type when someone, for example, someone takes the object back out of the list and assigns it to the specific type they’re expecting. The compiler is making a promise that it guarantees this will succeed, so it has to restrict what goes in and out.

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

Sidebar

Related Questions

Possible Duplicate: Java - Regex problem I have list of URLs of types: http://www.example.com/pk/etc
Possible Duplicate: Type List vs type ArrayList in Java Why is it recommended to
Possible Duplicate: Java - Distinct List of Objects i have a sorted array of
Possible Duplicate: Java: convert List<String> to a join()d string Having this: List<String> elementNames =
Possible Duplicate: When to use a List over an Array in Java? As the
Possible Duplicate: Implement linked list in php We can make link list in Java,
Possible Duplicate: Python list append behavior Why does this code: x = [[]]*3 x[0].append('a')
Possible Duplicate: How to list my app downloads I created an android app that
Possible Duplicate: Get a list of dates between two dates This is my sql:
Possible Duplicate: Concatenating a C# List of byte[] I have a list of byte[]

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.