Chapter 13: Section Parameterized Methodes: Page 507-509
The author changes some methodes in the BinaryTree class to parameterized methodes in order to be able to add objects that are of types that are subclasses of a type parameter.
I don't understand what that is good for. The methodes also excepted types, which are subclasses of the type parameter supplied to the class befor making this change. The code seems to compile and run in the same way before and after the change. I put some code fragments below.
code before the parameterization of the methodes:
Code:
public class BinaryTree<T extends Comparable<? super T>> {
...
public void add(T value) {
...
}
public void add(T value, Node node) {
...
}
...
}
code after the parameterization of the methodes:
Code:
public class BinaryTree<T extends Comparable<? super T>> {
...
public <E extends T> void add(E value) {
...
}
public <E extends T> void add(E value, Node node) {
...
}
...
}
What is the diffrence between this two codes? Can't I supplement T with a subclass of T anyway? Don't they work exactly in the same way?