Since generics are invariant. The following code produces a compile time error incompatible types:
Stack<String> stackOfStrings = new Stack<String>();
Stack<Object> stackOfObjects = stackOfStrings;
Why then is the code below that produces an array of a stack of strings acceptable to the compiler and taught in textbooks such as Algorithms, 4th Edition by Robert Sedgwick and Kevin Wayne, pg. 158:
Stack<String>[] a = (Stack<String>) Stack[];
EDIT: the above snippet taken directly from the textbook is actually (sorry for the mistake):
Stack<String>[] a = (Stack<String>[]) new Stack[N];
(Stack<String>) new Stack[10];.