Java: return type of concrete method of abstract class becomes raw

339 Views Asked by At

The following code gives two warnings when compiled with -Xlint. One of them states that type C<Double> is required but C was found, even though the return type is very clearly C<Double>.

I found a solution that gets rid of both warnings, but I don't understand (and want to know) why that was a warning in the first place and why it disappeared.

Code:

public class Test {
    public static void main(String[] args) {
        B b = new B();
        thing(b);
    }

    static void thing(A a) {
        C<Double> c = a.a();
    }
}

abstract class A<T> {
    C<Double> a() {
        return new C<Double>();
    }
}

class B extends A<String> {}

class C<T> {}

Warnings:

Test.java:7: warning: [rawtypes] found raw type: A
    static void thing(A a) {
                      ^
  missing type arguments for generic class A<T>
  where T is a type-variable:
    T extends Object declared in class A
Test.java:8: warning: [unchecked] unchecked conversion
        C<Double> c = a.a();
                         ^
  required: C<Double>
  found:    C
2 warnings

Solution to the warnings:

I changed static void thing(A a) to static void thing(A<?> a).

But:

There is no reason for a.a() to return C rather than C<Double> which is the return type stated everywhere. I don't see why A being raw should make the return type of all of its methods raw, when the type parameters of A don't affect .a();

0

There are 0 best solutions below