sksamuel
sksamuel

Reputation: 16387

Java Generics with Wildcards Not Compiling

I'm having trouble understanding the finer points of Java generics with wildcards. specifically, why doesn't this compile.

public class Test {

    abstract class Function<A, B> {
        abstract B call(A a);
    }

    interface PropertyType {
        String bubbles();
    }

    class Apartment implements PropertyType {
        @Override
        public String bubbles() {
            return "bubbles";
        }
    }

    public void invokeFunctionOnAList() {
        List<Apartment> apts = new ArrayList<Apartment>();
        functionLoop(apts, new Function<Apartment, String>() {

            @Override
            String call(Apartment a) {
                return a.bubbles();
            }
        });
    }

    public void functionLoop(List<? extends PropertyType> list, Function<? extends PropertyType, String> t) {
        for (PropertyType p : list) {
            t.call(p);
        }
    }
}

Upvotes: 0

Views: 199

Answers (3)

Thomas
Thomas

Reputation: 10065

Your compiler does not know if you are using same type in List and Function. Therefore you have to tell him this.

Try this:

public <C extends PropertyType>void functionLoop(
                         List<C> list, Function<C, String> t) {
  for (C p : list) {
    t.call(p);
  }
}

Upvotes: 1

Flavio
Flavio

Reputation: 11977

The most formally correct way to put that code actually is

public <C extends PropertyType> void functionLoop(
        List<C> list, Function<? super C, String> t) {
    for (C p : list) {
        t.call(p);
    }
}

The best explanation of generics I found was on "Effective Java" by Joshua Bloch. You can find a small excerpt which can relate to your example in this presentation.

Upvotes: 3

Vladimir Ivanov
Vladimir Ivanov

Reputation: 43098

because call(Apartment a) should get Apartment object as a parameter and you pass a PropertyType object. THough Apartment is-a PropertyType, but PropertyType is-NOT-a Appartment.

Upvotes: 0

Related Questions