4

Since we can do

ArrayList<?> l = new ArrayList<Integer>();

Can we say ArrayList<?> is superclass of ArrayList<Integer> ? Is the above example hence depicts polymorphism ?

Update: In general , If A<T> is superclass of B<T> , then

A<?> obj = new B<Integer>();

Then is right to say A<?> is super class of B<Integer> ?

Number945
  • 3,732
  • 4
  • 34
  • 61
  • I thought the unbounded wildcard is already covered somewhere in SO, but apparently it's not the case. – justhalf Mar 24 '17 at 07:15
  • No, [`AbstractList`](https://docs.oracle.com/javase/8/docs/api/java/util/AbstractList.html), [`AbstractCollection`](https://docs.oracle.com/javase/8/docs/api/java/util/AbstractCollection.html), and `Object` are all superclasses of [`ArrayList`](https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html). – Andreas Mar 24 '17 at 07:24
  • 1
    Generics are not covariant, that means that a `List` is not a `List`. See [this answer to a similar question](http://stackoverflow.com/questions/2745265/is-listdog-a-subclass-of-listanimal-why-arent-javas-generics-implicitly-p/2745301#2745301). – MC Emperor Mar 24 '17 at 07:41

2 Answers2

2

TLDR :

Can we say ArrayList<?> is superclass of ArrayList<Integer> ?

NO but we can say it is supertype of ArrayList<Integer>

Then is right to say A<?> is super class of B<Integer> ?

YES and we can also say it is supertype of B<Integer>

Also note that inheritance is used only in context of subclass or super class and not subtype or supertype.

Quick Reference


First thing you need to understand is class is a type in java. We will see below that wherever it is valid to use the term sub/super class , it is valid to use sub/super type but not vice versa

Let me now define superclass. According to JLS 8.1.4

Given a (possibly generic) class declaration for C<F1,...,Fn> (n ≥ 0, C ≠ Object) (Fi here is type parameter), the direct superclass of the class type C<F1,...,Fn> is the type given in the extends clause of the declaration of C if an extends clause is present, or Object otherwise.

Let C<F1,...,Fn> (n > 0) be a generic class declaration. The direct superclass of the parameterized class type C<T1,...,Tn>, where Ti (1 ≤ i ≤ n) is a type (argument), is D<U1 θ,...,Uk θ>, where D<U1,...,Uk> is the direct superclass of C<F1,...,Fn>, and θ is the substitution [F1:=T1,...,Fn:=Tn].

A class A is a subclass of class C if either of the following is true:

a) A is the direct subclass of C
b)There exists 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 said to be a superclass of class A whenever A is a subclass of C.

To explain the thing in little simple words , consider a simpler example : C<F1,...,Fn> be ArrayList<T> and C<T1,...,Tn> will be say ArrayList<Integer> in above definition. T is type parameter and we instantiate it with Integer which is type argument.

[ What did we meant by θ is the substitution [F1:=T1,...,Fn:=Tn] ? ]

Now, Does A<?> comes in extends clause of A<Integer> ? (I know it is stupid to ask such a structure , but let us be strict with definition) . No it does not. Generally , in extends we mention a different class type altogether.

Now, let us see definition of sub/super type. By JLS 4.10.2

Given a generic type declaration C<F1,...,Fn> (n > 0), the direct supertypes of the parameterized type C<T1,...,Tn>, where Ti (1 ≤ i ≤ n) is a type, are all of the following:

  1. D<U1 θ,...,Uk θ>, where D<U1,...,Uk> is a generic type which is a direct supertype of the generic type C<T1,...,Tn> and θ is the substitution [F1:=T1,...,Fn:=Tn].

  2. C<S1,...,Sn>, where Si contains Ti (1 ≤ i ≤ n) (§4.5.1).

  3. The type Object, if C<F1,...,Fn> is a generic interface type with no direct superinterfaces.

  4. The raw type C.

Now by this definition , according to point 2

C<S1,...,Sn>, where Si contains Ti (1 ≤ i ≤ n) (§4.5.1).

? contains Integer(Reference). Hence, this makes A<?> supertype of A<Integer>.

You can easily see the point 1 of this definition, includes the subclass definition in itself.

The second part of question , where We have said A<T> extends B<T> , makes it fall under both definitions.

Lastly, we see what inheritance mean. By JLS 8.4.8

A class C inherits from its direct superclass all concrete methods m (both static and instance) of the superclass for which all of the following are true: [...]

Number945
  • 3,732
  • 4
  • 34
  • 61
  • Hi Ben could you answer my question about this line `D, where D is the direct superclass of C, and θ is the substitution [F1:=T1,...,Fn:=Tn].`? Thanks! https://stackoverflow.com/questions/51434267/confusion-over-generics-subtyping-in-java-specs – mzoz Jul 20 '18 at 02:36
0

Here inheritance applies to generic classes not generic arguments

that is to say ArrayList<?> is not super class of ArrayList<Integer>

It is same class with different type parameters . If you would have used any other type parameter than <?> there the code would have not worked. even with superclass of Integer

harsha kumar Reddy
  • 961
  • 1
  • 13
  • 28