Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Generics problem

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Generics problem

    abstract class ABS<K extends Number>
    {
    	public abstract <K> K useMe(Object k);// understood ..  
    	public abstract <K> ABS<? extends Number> useMe(ABS<? super K> k)	; //1
    	public abstract <K> ABS<? super Number> useMe(ABS<? extends K> k); //2
    	public abstract <K> ABS<K> useMe(ABS<K> k);// understood ..  
    }

    1 and 2 this both should not work because K can be anything here....

    can anyone please explain???

    Thanks in advance...


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Generics problem

    I'm not entirely sure what you're asking for,

    #2-4 should all fail to compile (numbering 1 is the top-most method, 4 is the bottom-most)

    abstract class ABS<K extends Number>
    {
    	public abstract <K> K useMe(Object k);// compiles fine...
    	public abstract <K> ABS<? extends Number> useMe(ABS<? super K> k)	; // fails to compile because ABS<? super K> could be declared to be something like ABS<Object>, which is always impossible. Also fails for the same reason #3 fails (generic type hiding)
    	public abstract <K> ABS<? super Number> useMe(ABS<? extends K> k); // fails to compile because K here is hidden by the method generic typing, and isn't the class generic typing. Will also fail because it must return ABS<? super Number>, which fails for the same reason #2 fails.
    	public abstract <K> ABS<K> useMe(ABS<K> k);// fails to compile by the same reason as #3.
     
    	public abstract <V> ABS<? extends Number> useMe(ABS<? super V> k); // equivalent statement for #2.
     
    	public abstract <V> ABS<? super Number> useMe(ABS<? extends V> k); // equivalent statement for #3.
     
    	public abstract <V> ABS<? extends Number> useMe(ABS<V> k); // equivalent statement for #4.
    }
    Last edited by helloworld922; January 22nd, 2011 at 03:05 PM.

Similar Threads

  1. Generics
    By _lithium_ in forum What's Wrong With My Code?
    Replies: 21
    Last Post: December 6th, 2010, 07:08 PM
  2. Generics collections and reflection
    By juza in forum Collections and Generics
    Replies: 2
    Last Post: November 30th, 2010, 03:14 AM
  3. Trying to somehow Compare Generics
    By Omega_ryan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 14th, 2010, 12:58 PM
  4. Generics and Reflection
    By Kassiuz in forum Collections and Generics
    Replies: 3
    Last Post: March 15th, 2010, 09:32 AM
  5. Custom Java stack class (with generics) problem
    By TBBucs in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 7th, 2010, 02:25 AM

Tags for this Thread