Java Reference
In-Depth Information
Manager[] managers = { new Manager("Jane", 1), new Manager("Joe",
3),
new Manager("Freda", 3), new
Manager("Albert", 2)};
for(Manager manager: managers){
people.add(manager);
System.out.println("Added "+ manager);
}
System.out.println();
listAll(people.sort());
}
// List the elements in any linked list
public static void listAll(LinkedList<?> list) {
for(Object obj : list) {
System.out.println(obj);
}
}
}
Directory "TryFlexibleBinaryTree"
You also need the modified version of BinaryTree<> and the source file for LinkedList<> . The output
should be:
Added Manager Jane level: 1
Added Manager Joe level: 3
Added Manager Freda level: 3
Added Manager Albert level: 2
Manager Albert level: 2
Manager Freda level: 3
Manager Jane level: 1
Manager Joe level: 3
How It Works
The output demonstrates that the BinaryTree<T> type works with a type argument that implements Com-
parable<T> even when the implementation is inherited. The use of a wildcard with a lower bound as the
parameter for the constraint adds the flexibility to allow inherited implementations of the constraint type.
This is usually what you want for any constraint on a type argument for a parameterized type, so you
should always use this pattern with constraints for all your generic types unless you have a reason not to.
More on the Class Class
I mentioned in Chapter 6 that the Class class is not an ordinary class type; rather, it's defined as a paramet-
erized type. The Class<> object for a given type such as Person or java.lang.String in your application
is produced by supplying the type as the argument for the generic type parameter, so of type Class<Per-
son> and Class<java.lang.String> in these two instances. Because these class types are produced from
a generic type, many of the methods have parameters or return types that are specifically the type argument
Person or java.lang.String in the two examples I've cited.
Search WWH ::




Custom Search