Results 1 to 4 of 4
  1. #1
    JKoder70014 is offline Member
    Join Date
    Jul 2011
    Posts
    6
    Rep Power
    0

    Default Regarding java generics

    I get the compiler error in the below code snippet.

    Queue<? super Number> q1 = null;
    Queue<? extends Integer> q2 = null;
    q1 = q2;

    The compiler error should not happen in this time because, q2 is anyway the subclass of q1.

    ***Error** = Type mismatch: cannot convert from Queue<capture#2-of ? extends Integer> to Queue<? super Number>


    ***My Understanding of the above code**

    As per my understanding:

    (1) In java, you can assign any subtype to its supertype variable place holder. (For Example : Object o = new String();)
    (2) So, in the below lines of code,
    (a) Queue<? super Number> q1 // It can be a Number or an Object
    (b) Queue<? extends Integer> q2 // It can be a Integer or anything below the Integer hierarchy. ***** Integer is a subclass of Number*****.

    So, I assume that since Number is a parent class of Integer, the Number class can hold its child class (Integer) or anything below the hierarchy.

    So, i am not clear , what I have missed, ....!

    can anybody please clarfiy me on this.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Why is q2 a subclass of q1? (or Queue<? extends Integer> a subtype of Queue<? super Number>) Aren't subclasses supposed to have all the behaviour of their parents? What about adding instances of Float?

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,438
    Rep Power
    18

    Default

    A Queue of Integers is not a Queue of Numbers.
    If it were and your assignment was allowed then (riffing off pbrockways post):
    Java Code:
    Queue<? super Number> q1 = null;
    Queue<? extends Integer> q2 = null;
    q1 = q2;
    q1.add(new Float(1.2f));  // 1
    
    Integer i = q2.remove(); // 2
    what should the compiler do at point 1?
    You've declared q2 as a Queue of Integers and now you are adding a Float to it?
    And at point 2? Wouldn't that cause a bit of a *bang* as the JVM fails to cast a Float to an Integer?

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    We seem to have lost the OP...

    For more google "java tutorial monarch". The reason for the last term is that the Tutorial considers modelling an "animal cage" and the disastorous consequence of confusing butterfies with lions when it comes to adding them to the right sort of cage.

    The op's confusion is perfectly understandable - after all an int queue is a specialized sort of number queue. But that sort of specialisation is not what subtyping is getting at.

Similar Threads

  1. Java Generics
    By learn2java in forum Advanced Java
    Replies: 4
    Last Post: 05-05-2011, 07:11 AM
  2. Generics in java
    By ajeeb in forum Advanced Java
    Replies: 5
    Last Post: 01-23-2009, 04:56 PM
  3. Question about java generics
    By Arrowx7 in forum New To Java
    Replies: 1
    Last Post: 08-14-2008, 02:37 AM
  4. Java confused over Generics?
    By Bibendum in forum New To Java
    Replies: 3
    Last Post: 12-26-2007, 06:23 AM
  5. Java Generics (an introduction)
    By Java Tutorial in forum Java Tutorial
    Replies: 0
    Last Post: 11-27-2007, 06:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •