Java wildcard inheritance
So how can we do
ArrayList<?> l = new ArrayList<Integer>();
Can you tell which ArrayList<?>
is the superclass ArrayList<Integer>
? Is the above example therefore displaying polymorphism?
Refresh . In general, If A<T>
is a superclass B<T>
, then
A<?> obj = new B<Integer>();
Then we can say what A<?>
is super class B<Integer>
?
Here inheritance applies to generic classes, not generic arguments
that is, it is ArrayList<?>
not a superclassArrayList<Integer>
This is the same class with different type parameters. If you were using any other type parameter than the <?>
code would not work. even with superclass Integer
Can you tell which
ArrayList<?>
is the superclassArrayList<Integer>
?
Not.
ArrayList<?>
is the same class as ArrayList<Integer>
. There is only one declaration for ArrayList<?>
and ArrayList<Integer>
, but these are different types:
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
<?>
means that the generic type of the parameter can be any type.
Is the above example therefore displaying polymorphism?
I think yes.
What you are basically doing is assigning an instance of a type to a variable of a different type. These are "morphs".
Then it's fair to say what
A<?>
is super classB<Integer>
?
Yes. This can be shown with the following code:
// BClass extends AClass
AClass<?> a = new AClass<String>();
BClass<Integer> b = new BClass<Integer>();
// prints true
System.out.println(b.getClass().getSuperclass().equals(a.getClass()));
This is because the type erasure occurs at runtime, so generic types are not displayed. Adoption A<T>
is superclass B<T>
means that A<Anything>
is a superclass B<Anything>
.
Let me define a superclass. According to JLS 8.1.4
Given the (possibly generic) class declaration for
C<F1,...,Fn>
(n โฅ 0, C โ Object), the direct superclass of the type classC<F1,...,Fn>
is the type specified in the extends clause of the C declaration if extends, or Object otherwise.Let
C<F1,...,Fn>
(n> 0) be a generic class declaration. The direct superclass of a parameterized class typeC<T1,...,Tn>
, where Ti (1 โค i โค n) is the type, isD<U1 ฮธ,...,Uk ฮธ>
, whereD<U1,...,Uk>
is the direct superclassC<F1,...,Fn>
, and ฮธ is the substitution [F1: = T1, ..., Fn: = Tn].Class A is a subclass of class C if one of the following conditions is true:
a) A is a direct subclass of C
b) There is a class B such that A is a subclass of B and B is a subclass of C, applying this definition recursively.Class C is called the superclass of class A whenever A is a subclass of C.
The only question that now remains is, can a template be considered a type?
The answer is missing as in JLS 4.1 again.
There are two types of types in the Java programming language: primitive types (ยง4.2) and reference types (ยง4.3).
Hence, the answer is:
Wildcarding as type arguments never makes a class a superclass. It is not considered in the inheritance chain.
Note. Type and Type Arguments are two different things.