0

I have some code that doesn't compile with JDK10.
I reduced it to the example below:

abstract class Base<C extends Comparable<C>> {
    C c;
    protected Base(C c) {
        this.c = c;
    }
}

class Derived<O extends Object> extends Base<String> {
    Derived(String s) {
        super(s);
    }
}

class Scratch {
    private static void printString(String s) {
        System.out.println(s);
    }

    public static void main(String[] args) {
        var d = new Derived("s");
        printString(d.c);
    }
}

When I call printString(d.c) the compiler complains with Error:(21, 22) incompatible types: java.lang.Comparable cannot be converted to java.lang.String.

If I change
class Derived<O extends Object> extends Base<String> {
to
class Derived extends Base<String> {
the code works as intended.

How can I fix the code (so that d.c is of type java.lang.String) while keeping the type parameter O on the Derived type?

0

1 Answer 1

5

When you declare class Derived<O extends Object> but then initialize it as just new Derived, it becomes a raw type, which kills all of the generics. To fix it, initialize it with new Derived<Object> or new Derived<String> or something instead.

Sign up to request clarification or add additional context in comments.

3 Comments

Great, thanks. Would it be safe to say that this is a limitation due to how generics are implemented in Java? After all, while I'm not providing information about the type parameter O, it should be clear that C is a String.
I'd agree with that. Even without O, there should indeed be no ambiguity about C. Perhaps some future version of Java will change this.
"Great, thanks. Would it be safe to say that this is a limitation due to how generics are implemented in Java? " ... Yes, this is Java working as intended. Generics were retrofitted into the language with release 5. Accepting raw types in valid expressions was necessary to assure backward compatibility with the large code base that was written before generics were present in the language and to assure interoperability between new generics code and legacy pre-generics code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.