This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Generics - Doubt in using "extends" and "super"  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I using the K&B Book for my SCJP preparation.

In the Generics chapter, I found a code fragment like

public void addAnimal(List<? extends Animal> animals) {
animals.add(new Dog()); // NO! Can't add if we
// use <? extends Animal>
}

and

public void addAnimal(List<? super Dog> animals) {
animals.add(new Dog()); // adding is sometimes OK with super
}

Why is that adding to list when using <? extends Animal> creates a compilation error and why the other <? super Dog> doesn't give a compilation error??
When we are using "extends" its like we are going to add the subtypes of Animal. So casting shouldn't be a problem when we retrieve the objects from the list??

I have written a code (java 1.5) like

package chap7;

import java.util.ArrayList;
import java.util.List;

public class GenericsSample{
public GenericsSample() {
}

public static void main(String[] args) {
GenericsSample genericsSample = new GenericsSample();
Plant p = new Plant();
List<Plant> plants = new ArrayList<Plant>();
plants.add(p);
plants.add(new Plant());

List<Flower> flowers = new ArrayList<Flower>();
flowers.add(new Flower());
flowers.add(new Flower());


Tree tree = new Tree();
tree.check(plants);
tree.check(flowers);

}

}

class Tree {
public void check(List<? extends Tree> t){
t.add(new Plant()); // have added - no compilation error
}
}

class Plant extends Tree{
}

class Flower extends Plant{
}

But i dont get any compilation error when i tried adding
t.add(new Plant()); inside the method.

Is it something i am doing wrong,Confused with the methods. Can anyone help out in finding the reasons??

Thanks,
Jayashree.
 
Sheriff
Posts: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well for your example if I pass ArrayList<Flower> to the method check(List<? extends Tree> t), then what would happen?? You would add a Plant into a Flower list which is not allowed. A Plant is not a flower. This is why it is not allowing you to add any elements to the list.

Although nothing would happen when you add the Plant into the list, the exception will occur when you retrieve elements from the list. See this

List<Flower> listFlower = new ArrayList<Flower>();
tree.check(listFlower); //now the check method would add a Plant into this list
Flower f = listFlower.get(0); //BOOM! ClassCastException, cannot convert Plant into Flower

This is why it shows you an error...
 
Jayashree Kesavan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ankit,

Thank you so much for your explanation.I got the logic in it.

Thanks,
Jayashree.
 
Mo-om! You're embarassing me! Can you just read a tiny ad like a normal person?
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!