Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

K & B Mock exam Generics doubt

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi swati,

3. public static <T>List<? extends T> backwards(List<T> input)
4. public static <T> List<? super T> backwards (List<T>input)

I think both the options are correct, since in 3 we are returing a list of T or or one of it's child class. And in the 4, we are returning a list of T or one of it's parent class.

We are not calling that function from anywhere in that fragement of code. I think your confusion is like this "How can we return a reference of parent class where a child class reference is expected" for option 4.

And option 3 is correct anyway since we can return reference of child class where parent class reference is expected.
 
Swati Khanna
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My confusion is that in the method backwards we are returning List<T>
and the return type of the method is List<? extends T> or List<? extends super> in option 3 and 4.
So isn't it like are trying to assign a List<Object> to List<String>
or vica versa. I hope I make sense.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, i think List<? extends T> is different from List<Object>.

List<Object> can only be assigned List implementations with generic type Object where as List<? extends T> can take any implementation with generic type T or anything that extends T or implements T(in case of T being an interface).

Second, return type is List<? extends T> and you are returning List<T>
List<? extends T> t = ArrayList<T>(); is possible I think.

Similarly the case with <? super T>. Hope this helps!!!
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that you have a very interesting question... And I don't think that the last two implementations are very useful. Look at the classes Animal, Dog and Cat (extend Animals).

Now I input a List of Animals into the 3rd backwards method, and the return type is List<? extends Animal>. Now, what is my return type? A list of Animals? a list of Dogs? a list of Cats and Dogs? you can't tell, but you also can't put the outputted list in a List<Animal> (because it could be a List<Dog> )! You can only put the list in a List<? extends Animal> or an Object.

The same goes for the 4th backward method (is it a List<Object> or List<Animal>?). I think that it is more useful to use 2 specified generics instead of just one, like this:



With this code you can input a List<Animal> and get a List<Dog>, List<Cat> or List<Beagle> in return (it's probably not perfect but illustrates the use of multiple generics).

[ September 19, 2007: Message edited by: Xander Steinmann ]
[ September 19, 2007: Message edited by: Xander Steinmann ]
 
Swati Khanna
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Yogvinder,


So does 'nt this mean that you are trying to write
List<String> t = ArrayList<Object>();
as String extends Object where T is Object.

I am still not clear.
 
Xander Steinmann
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So does 'nt this mean that you are trying to write
List<String> t = ArrayList<Object>();
as String extends Object where T is Object.
I am still not clear.


No, "List<? extends T> t" means that t is a list of Objects that extend T. If you use T=Object then it says:
List<? extends Object> t = new List<Object>();
This doesn't mean that you can fill in just anything for <? extends Object> (like String). List t has as type "? extends Object", not String. But you CAN say: "List<? extends Object> t = new ArrayList<String>();"
 
Yogvinder Singh Ghotra
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.. Swati..

Please go through the tutorial
http://java.boot.by/scjp-tiger/ch06s04.html

Here in java ranch, You can rad the thread
https://coderanch.com/t/265228/java-programmer-SCJP/certification/Generic-methods-wildcards

Hope this will help. If not, do tell me.
 
    Bookmark Topic Watch Topic
  • New Topic