Thomas
Thomas

Reputation: 1123

Why does an "Incompatible types" compile error turn into a warning when a class is made generic?

I have this code, which fails to compile:

import java.util.ArrayList;
import java.util.List;

public class TypeSafetyTest {

    public static void main(String[] args) {
        TypeSafetyTest test = new TypeSafetyTest();
        test.run();
    }

    private void run() {
        Car car = new Car();
        List<String> wheelWeights = car.getWheelWeights();
    }

    private class Car {
        List<Double> wheelWeights = new ArrayList<Double>();

        public List<Double> getWheelWeights() {
            return wheelWeights;
        }

        public void setWheelWeights(List<Double> wheelWeights) {
            this.wheelWeights = wheelWeights;
        }
    }
}

It gives an "Incompatible types" error on the line:

List<String> wheelWeights = car.getWheelWeights();

However, if I change the line:

private class Car {

to

private class Car<T> {

then the code compiles successfully with the warning "Unchecked assignment" on the line that used to have the compile error. Why is this so? I was expecting it to give a compile error in both cases.

Upvotes: 2

Views: 3584

Answers (2)

kosa
kosa

Reputation: 66677

One issue I see is:

 public List<Double> getWheelWeights() {
            return wheelWeights;
        }

returns Double type List, but you are assigning it to List<String>

The reason why you are not seeing compile error when you change Car<T> is, because your instance creation is raw type, which is telling compiler that ignore type safety at compile time, due to backward compatability.

Car<Object> car = new Car<Object>();

If you change your object like this, then you will see compiler throwing the message again.

Please refer JLS 4.8 for more information.

Here is example from JLS:

    class Outer<T>{
        class Inner<S> {
            S s;
        }
    }

Outer.Inner<Double> x = null;  // illegal

It is not possible to access Inner as a partially raw type (a "rare" type), because Outer itself is raw, hence so are all its inner classes including Inner, and so it is not possible to pass any type arguments to Inner

The use of raw types is allowed only as a concession to compatibility of legacy code. The use of raw types in code written after the introduction of generics into the Java programming language is strongly discouraged. It is possible that future versions of the Java programming language will disallow the use of raw types.

I would suggest reading Generics (Updated)

Upvotes: 3

Antimony
Antimony

Reputation: 39511

When you change Car to Car<T>, Car becomes a generic class. Therefore, your car object now has the raw type Car, since you didn't provide any type parameters. And nonstatic non inherited members of a raw type are also a raw type. This means that getWheelWeights() is now returning a raw type. And since it's a raw type, it does an unchecked conversion, meaning you only get a warning, not an error.

You can fix this by providing a type argument for car. For example

Car<Object> car = new Car();

Note that if you want the type safety benefits of generics, you shouldn't use raw types at all. They are just a compatibility fix for pre-generic code, hence the contagiousness and lack of checking for raw types.

Upvotes: 3

Related Questions