Java Reference
In-Depth Information
return this.name.compareTo(person.name);
} else if(this.getClass().getName().equals("Manager")) {
return 1;
} else {
return -1;
}
}
Directory "TryParameterizedConstructor"
With this change, the output puts Ann the non-managerial person in her place. What's more, the managers
are listed after the non-managers as it should be, since they are higher up the tree.
PARAMETERIZED TYPES AND INHERITANCE
You can define a class as a subclass of an instance of a generic type. For example, you could derive a new
class from LinkedList<Person> or from type BinaryTree<String> . Methods and fields are inherited from
the base class in the usual way. However, you can encounter complications because of the way the compiler
translates methods that involve type arguments into bytecodes, so let's first understand that process.
Each method that involves parameters and/or the return value type specified by a type argument is trans-
lated by the compiler to a method with the same name, but with the type of each parameter whose type is
a type variable replaced by its leftmost bound. Where the type of the return value is a type variable, then
that, too, is replaced by its leftmost bound. Casts are inserted in the body of the translated method where
necessary to produce the actual types required. An example will help clarify this.
Earlier you saw a version of the LinkedList<> type defined as:
public LinkedList<T extends Object & Serializable> {
public void addItem(T item) {
// Code for the method...
}
// More code for the type definition...
}
If you define an object of type LinkedList<String> , notionally the addItem() method for the object
looks like this:
public void addItem(String item) {
// Code for the method...
}
However, the compiler translates the addItem() method to the following:
public void addItem(Object item) {
// Code for the method...
Search WWH ::




Custom Search