• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on generics. Please explain.

 
Greenhorn
Posts: 20
Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The options are:

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<? extends T> List<T> backwards(List<T> input)
g. public static<? super T> List<T> backwards(List<T> input)

The correct answers are a, b, d and e. Please explain why are they correct.
 
Ranch Hand
Posts: 74
5
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not very practiced with generics so please correct me if I'm wrong.

The first block after the static identifier declares your type which is T. You can use any character but T is usually chosen for readability. The following method header is actually working as well:



For the sake of clarity you should stick to T though.

The question mark does not describe a valid type same as you can't use it as a field name.

does not compile.

That's why f and g are incorrect. c is incorrect because it doesn't work with the method content. The question mark stands for any object that may be used in the context. Option c expects that the items in input are a super type of T. So T extends the items in your list "input". You do however try to add every item from input to a list output which is defined as containing only items that are of type T. You'll see a respective error description if you copy the code including option c as the method header to your IDE (like Eclipse or NetBeans).

To make it more tangible let's say we have a couple of classes:

Vehicle extends Object
Car extends Vehicle
Roadster extends car

Let's assume you implement your method for Type Car and the method header is option c:



input would be expected to contain items that are a super type of Car. So instances of Object and Vehicle are valid items that can be added to the list. output is declared as containing items of type T. Car and Roadster are both valid options since Roadster can be cast as Car. If output was declared as "List<? extends T>();" only Roadster instances could be added to the list since Car does not extend Car.

It appears you should try to learn the basics of generics first and then ask specific questions if there is something you don't understand. This way you'll likely receive more replies here. Good luck!

Pritha Banerjee wrote:

The options are:

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<? extends T> List<T> backwards(List<T> input)
g. public static<? super T> List<T> backwards(List<T> input)

The correct answers are a, b, d and e. Please explain why are they correct.

 
Ranch Hand
Posts: 175
17
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OPTION C: public static<T> List<T> backwards(List<? super T> input) fails because of this for loop

For example, if the type parameter (T) is Number, the for loop will not successfully process Number supertypes because a Number supertype cannot be assigned to a Number, for example Number t = new Object()

OPTION B: public static<T> List<T> backwards(List<? extends T> input) succeeds because if the type parameter (T) is Number, the for loop will successfully process Number subtypes because a Number subtype can be assigned to a Number, for example Number t = new Integer(1)

OPTION A: public static<T> List<T> backwards(List<T> input) succeeds for a similar reason

OPTION D: public static<T> List<? extends T> backwards(List<T> input) succeeds because you're simply indicating that the method returns a restricted List, for example, if the type parameter (T) is Number, the method may return a List of Integer or a List of Long, however, the compiler will restrict what you can add to the returned List - you can add only nulls; however, you shouldn't add anything to the returned List.

OPTION E: public static<T> List<? super T> backwards(List<T> input) succeeds because you're simply indicating that the method returns a restricted List, for example, if the type parameter (T) is Integer, the method may return a List of Number, however, the compiler will restrict how you retrieve elements from the returned List - you can retrieve elements as only as Objects; however, you shouldn't retrieve anything from the returned List.

OPTION F: public static<? extends T> List<T> backwards(List<T> input) fails because a generic method declaration must specify a type parameter, for example T (general type), E (element type), K (key type), V (value type). ? is not a type parameter

OPTION G: public static<? super T> List<T> backwards(List<T> input) fails for the same reason.
 
Pritha Banerjee
Greenhorn
Posts: 20
Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it , thanks
 
Well behaved women rarely make history - Eleanor Roosevelt. tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic