w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Is this generics example from Bruce Eckel's "Thinking in Java" wrong?

Polymorphic assignments applies only to the base type, not the generic type parameter. You can say

List<Animal> aList = new ArrayList<Animal>(); // yes

You can't say

List<Animal> aList = new ArrayList<Dog>(); // no

Wildcard syntax allows a generic method, accept subtypes (or supertypes) of the declared type of the method argument:

void addD(List<Dog> d) {} // can take only <Dog>
void addD(List<? extends Dog>) {} // take a <Dog> or

When using a wildcard, List<? extends Dog>, the collection can be accessed but not modified.

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.