• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Generics

 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from Master Exam.


}


Choose the correct statement to be inserted.

A.public static <T> List<T> backwards(List<T>input)
B.public static <T> List<T> backwards(List<? extends T>input)
C.public static <T> List<T> backwards(List<? super T>input)
D. public static <T> List<? extends T> backwards(List<T>input)
E. public static <T> List<? super T> backwards(List<T>input)
F. public static List<? extends T> backwards(List<T>input)
G. public static List<? super T> backwards(List<T>input)


The correct answer is A,B,D,E.

I understand F,G are wrong because <T> is not defined.

A.public static <T> List<T> backwards(List<T>input)
This is correct because it returns <T> .

D. public static <T> List<? extends T> backwards(List<T>input)
E. public static <T> List<? super T> backwards(List<T>input)
D,E also seems correct as the return type is <? super T> and <? extends T >

B.public static <T> List<T> backwards(List<? extends T>input)
But i thought B is wrong as it is trying to add to a list which extends from T and you cannot add to such a list


C.public static <T> List<T> backwards(List<? super T>input)
And the explanation for C is given that ( which I thought should be for B is:

C is wrong because if the input can contain any supertype of C (like Object) those elements cannot be added to an output list of type T.

Can some please help me understand this.
I am not even clear with my own explanation.Still hazy with that.
[ September 19, 2008: Message edited by: Nabila Mohammad ]
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

Option B is same as option A, since the argument which is passed to the method will be of type T.

import java.util.*;

public class BackLister
{
// INSERT STATEMENT HERE
//A.public static <T> List<T> backwards(List<T>input)
public static <T> List<T> backwards(List<? extends 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<String> lst1=new ArraryList<String>( ); //#1
List<Object> lst2=new ArraryList<Object>( ); //#2

//more codes

System.out.println(backwards(lst1));
System.out.println(backwards(lst2));
}
}
when 1 and 2 passed to the method , T would either be of type String or of type Object.So option B is correct.

If we consider 2 parameters
public static <T> List<T> backwards(List<T>input,List<T> input1)
{ .... }

and call to backwads(lst1,lst2); would produce compiler error because T should be of same type.

? extends T denotes an unknown type that is subtype of T

public static <T> List<T> backwards(List<? extends T>input,List<T> input1)
{ .... }
call to backwads(lst1,lst2); would be successful because String is a subtype of Object.

? super T denotes a unkown that is supertype of T.In option C the supertype of T cannot be assigned to T in the for loop hence fails to compile.

option D,E returns subtype and supetype hence valid
[ September 19, 2008: Message edited by: sannuth kashikar ]
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am having the same issue as stated in this question.... I don't understand the reasoning behind options B and C.

I have a simple question to ask, which I think I am not clear on. When we have the below method, Option C:


Does this mean that return type List<T> output and the argument List<? super T> input
have the same place holder type?
Just to clarify my question: If <T> is Integer then output is List<Integer> and input will be List<Number> ???
OR both will be same: output is List<Integer> and input will be List<Integer>

Also for Option B:


If output is List<Number> is input List<Integer>
OR if <T> is Number output is List<Integer> is input List<Integer> ??


Thanks.
 
Meghna Bhardwaj
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Please reply to query below... I know some of you maybe thinking what a silly question, sorry...
but this will really help me clear up the generics concepts! Thanks.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello ..

I'm still learning generics too. I want to share my current experiment n conclusion so far. Any corrections and thoughts would be very appreciated !

Here's my experiment code :


And here's my temporary conclusions ..

Conclusion for the List<? super MyType>

What you can reference :
- Any lists without the specific parameterized type
- Any lists with any specific parameterized type which is equal or above the declared MyType

What you can add :
- Only the MyType objects

What you can get :
- Only Object type (just like the legacy lists), which means you'll have to downcast to make it a specific type

Cheers,
Albert Kam
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Albert
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nabila Mohammad wrote:
B.public static <T> List<T> backwards(List<? extends T> input)
But i thought B is wrong as it is trying to add to a list which extends from T and you cannot add to such a list



How did you came to such a conclusion. The code in the method doesn't try to add anything to the input list



Here what you are doing is, you have created a new list, then iterate over the input list i.e. get elements from it and add them into the output list. This is perfectly legal here. The list input will have elements which are a sub-type of T. So whatever it is, it will fit into a T reference. So that's why the loop will work. It is like doing something like this



Thus we are sure that elements in the input list are of type T or sub-type of T. Thus in the method (of your original code), we create a new list of type T and add all the elements of the input list into the new list and return it...
 
Proudly marching to the beat of a different kettle of fish... while reading this tiny ad
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic