• 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

generics question

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given:
import java.util.*;
public class Backlister {
//Insert here
{
List<T> output=new LinkedList<T>();
for(T t:input)
output.add(0,t);
return output;
}
}

Which of the following can be inserted at // to comile the code without error?

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)

Answer is A,B,D,E.

My doubt is why option D is correct??
Lets say T is Animal
So our function will become
public static<Animal> List<Dog>backwards(List<Animal>
{
---
---
return output // it will return list of animal but its return type is list of Dog
}

So is it valid?

Thanks,
Geeta Vemula.
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the source geeta?
 
geeta vemula
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry i forgot to write the source.
The source is learnkey MasterExam by K & B
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI geeta,

I think you have misunderstood it. You should not replace <? extends T> with Dog. It has to be <? extends Animal>
Lets take your example and replace T with Animal:



This code is perfectly valid. Compiles properly.
And remember here Animal is a type parameter not a class.
There is no difference if you use Animal in place of T. Actual type of T(or Animal) will depend on the call which makes call to this method:

List<Animal> input = new LinkedList<Animal>();
// Following two stmts dont compile because List<? extends Animal> cant be //assigned anything except List<?> or List<? extends Animal>
//List<Animal> list = backwards(input); this doesnt compile
//List<Dog> list2 = backwards(input); this also doesnt compile

List<?> list3 = backwards(input);// this is the correct usage
List<? extends Animal> list4 = backwards(input);// this is the correct usage

Hope this is clear! And i am not confusing you
[ December 05, 2008: Message edited by: M Srilatha ]
 
The airline is called "Virgin"? Don't you want a plane to go all the way? This tiny ad will go all the way:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic