Forum Stats

  • 3,817,474 Users
  • 2,259,340 Discussions
  • 7,893,791 Comments

Discussions

Generic Methods

952795
952795 Member Posts: 6
Hi everybody.

See screenshot:

http://img138.imageshack.us/img138/8782/50230399.png

Why answer "D" is correct?

Thank you.
Vitaliy.
«1

Answers

  • EJP
    EJP Member Posts: 32,920 Gold Crown
    edited Jul 30, 2012 7:19PM
    If you want an answer:

    1. Post your entire question here. Nobody is going to chase links for you.
    2. Don't crosspost.
    3. In a homework case like this, show some work of your own first. What do you think, and why?
  • 952795
    952795 Member Posts: 6
    This code compiles and runs without errors.
    import java.util.*;
    
    public class BackLister {
    
    	public static <T> List<? extends T> backwards(List<T> input) {
    		List<T> output = new LinkedList<T>();
    		for (T t : input) {
    			output.add(0, t);
    		}
    		return output; // Why do not occurs compile-time error ?
    	}
    
    	public static void main(String[] args) {
    		List<Long> longs = new LinkedList<Long>();
    		longs.add(Long.valueOf("356"));
    		System.out.println(backwards(longs));
    	}
    
    }
    But why?
    I expected that the code will not compile...
  • EJP
    EJP Member Posts: 32,920 Gold Crown
    Obviously the problem is with your expectations, not the code, so tell us about your expectations. What errors were you expecting, and where, and why?
  • gimbal2
    gimbal2 Member Posts: 11,949 Gold Trophy
    949792 wrote:
    But why?
    I expected that the code will not compile...
    "Why is water wet? I expected it to be dry!"

    What would your response be to this question?
  • 952795
    952795 Member Posts: 6
    edited Jul 31, 2012 4:39AM
    Please explain why the method backwards (which has type List <? Extends T>) appropriate to the value of output (which has type List <T>). After all, types List <T> and List <? extends T> are incompatible. Because I try this code :
    import java.util.*;
    public class BackLister {
    	public static <T> List<? extends T> backwards(List<T> input) {
    		List<T> output = new LinkedList<T>();
    		for (T t : input) {
    			output.add(0, t);
    		}
    		return output; 
    	}
    	public static void main(String[] args) {
    		List<A> list1 = new LinkedList<A>();
    		list1.add(new A());
    		List<A> listA = backwards(list1); // Error: Type mismatch: cannot convert from List<capture#2-of ? extends A> to List<A>
    		List<B> listB = backwards(list1); // Error: Type mismatch: cannot convert from List<capture#2-of ? extends A> to List<B>
    	}
    }
    class A {}
    class B extends A {}
    And I received an error.
  • EJP
    EJP Member Posts: 32,920 Gold Crown
    After all, types List <T> and List <? extends T> are incompatible.
    Too broad. You can assign in one direction but not the other.
    EJP
  • 952795
    952795 Member Posts: 6
    But why I get an error:
    cannot convert from List<capture#2-of ? extends A> to List<B>
    After all, class B extends A.
  • EJP
    EJP Member Posts: 32,920 Gold Crown
    But B doesn't extend ?. ? stands for some unknown class that extends A. Could be B, could be anything.
  • gimbal2
    gimbal2 Member Posts: 11,949 Gold Trophy
    EJP wrote:
    But B doesn't extend ?. ? stands for some unknown class that extends A. Could be B, could be anything.
    Takes me back to school. "A cow is an animal, but an animal is not necessarily a cow" :)
  • 952795
    952795 Member Posts: 6
    edited Jul 31, 2012 6:27AM
    Unfortunately you do not understand me.
    I'll try to explain another.

    I added the method "backwards2".
    import java.util.*;
    public class BackLister {
    	public static <T> List<? extends  T> backwards1(List<T> input) {
    		List<T> output = new LinkedList<T>();
    		for (T t : input) {
    			output.add(0, t);
    		}
    		return output; 
    	}
    	public static List<B> backwards2(List<A> input) {
    		List<A> output = new LinkedList<A>();
    		for (A t : input) {
    			output.add(0, t);
    		}
    		return output;  // Type mismatch: cannot convert from List<A> to List<B>	
    
    	}	
    	public static void main(String[] args) {
    	}
    }
    class A {}
    class B extends A {}
    The compiler shows an error:
    "Type mismatch: cannot convert from List <A> to List <B>"
    The reason for the error in the method "backwards2" is obvious.
    But I do not understand why the compiler does not show the same error in the method "*backwards1*".
This discussion has been closed.