problem about bounded-wildcard-Collection of common programming errors


  • olchauvin
    java generics bounded-wildcard
    Who could me explain this?I have these couple of classes:abstract class Animal {public void eat() {System.out.println(“Animal is eating”);} }class Dog extends Animal {public void woof() {System.out.println(“woof”);} }class Cat extends Animal {public void meow() {System.out.println(“meow”);} }And this is the action:import java.util.ArrayList; import java.util.List;public class TestClass {public static void main(String[] args) {new TestClass().go();}public void go() {List<Dog> animals = new

  • Shashi
    java generics bounded-wildcard
    There is any way to fix this situation (I have try to simplyfy the scenario as much as i could):public class Test {public static void main(String[] args) {/** HERE I would like to indicate that the CollectionGeneric can be of* something that extends Animal (but the constructor doesn’t allow* wildcards)*/CollectionGeneric<? extends Animal> animalsCollectionGeneric = new CollectionGeneric<Animal>();List<? extends Animal> animals = getAnimals();/* Why I cannt do that? */animalsCol

  • soc
    java generics inheritance covariance bounded-wildcard
    Consider the following Java class definitions:class Animal {} class Lion extends Animal {}When defining a covariant Cage for Animals I use this code in Java:class Cage<T extends Animal> { void add(T animal) { System.out.println(“Adding animal…”); } }But the following Java example …public static void main(String… args) {Cage<? extends Animal> animals = null;Cage<Lion> lions = null;animals = lions; // Works!animals.add(new Lion()); // Error! }… fails to compile wi

  • Paul Bellora
    java generics super bounded-wildcard pecs
    I came across PECS (short for Producer extends and Consumer super) while reading up on generics. Can someone explain to me how to use PECS to resolve confusion between extends and super?

  • Tomasz Nurkiewicz
    java generics wildcard type-parameter bounded-wildcard
    So why can we able to instantiate Pair but we can’t able to instantiate PairPair<T> p=new Pair<T>();VSPair<?> p=new Pair<?>();I know that <?> mean unknown type –> <? extends Object>but isn’t <T> mean the same thing —> <T extends Object>Anyone have an idea?

  • Robert Harvey
    java generics collections bounded-wildcard
    Please help me with this:If Lion IS-A Animal and given Cage<T>:Cage<? extends Animal> c = new Cage<Lion>(); // ok,butSet<Cage<? extends Animal>> cc = new HashSet<Cage<Lion>>(); // not okWhat I don’t see here?

  • Jeff Axelrod
    java generics bounded-wildcard
    This following is from generics tutorials:Say class R extends S,public void addR(List<? extends S> s) {s.add(0, new R()); // Compile-time error! }You should be able to figure out why the code above is disallowed. The type of the second parameter to s.add() is ? extends S — an unknown subtype of S. Since we don’t know what type it is, we don’t know if it is a supertype of R; it might or might not be such a supertype, so it isn’t safe to pass a R there. I have read it a few times but still

  • Jeff Axelrod
    java generics arraylist bounded-wildcard
    I’m having some trouble in the following code.public ArrayList<? extends IEvent> getEventsByDateRange(DateTime minStartTime, DateTime minEndTime) {ArrayList<? extends IEvent> returnedEvents = new ArrayList<GoogleEvent>();returnedEvents.add(new GoogleEvent());return (returnedEvents); }This return the following compilation error for the “returnedEvents.add(new GoogleEvent()); line of code”: The method add(capture#1-of ? extends IEvent) in the typeArrayList is not applicable for

  • Jeff Axelrod
    java generics bounded-wildcard
    I have a question about Generics in Java, namely using wildcards. I have an example class GenClass like this:public class GenClass<E> {private E var;public void setVar(E x) {var = x;}public E getVar() {return var;} }I have another simple class:public class ExampleClass {}I have written the following test class:public class TestGenClass {public static void main(String[] str) {ExampleClass ec = new ExampleClass();GenClass<ExampleClass> c = new GenClass<ExampleClass>();c.setVar(ec

Web site is in building