user2102697
user2102697

Reputation: 29

Avoid Unchecked Call Warnings

I have the following code (part of it)

public class Garage<T extends Vehicle>{

    private HashMap< String, T > Cars;
    private int Max_Cars;
    private int Count;

    public Garage(int Max_Cars)
    {
        Cars = new HashMap< String, T >();
        this.Max_Cars = Max_Cars;
        Count = 0;
    }

    public void add(T Car) throws FullException
    {
        if (Count == Max_Cars)
            throw new FullException();

        if (Cars.containsKey(Car.GetCarNumber()))
            return;

        Cars.put(Car.GetCarNumber(), Car);

        Count = Count + 1;

    }

.........
.........
}


public class PrivateVehicle extends Vehicle{

    private String Owner_Name;

    public PrivateVehicle(String Car_Number, String Car_Model, 
            int Manufacture_Yaer, String Comment, String Owner_Name)
    {
        super(Car_Number, Car_Model, Manufacture_Yaer, Comment);
        this.Owner_Name = Owner_Name;
    }
.........
.........
}

This is the main method (part of it)

    public static void main(String[] args) {

.........
.........

     Garage CarsGarage = new Garage(20);

.........
.........

     System.out.print("Owner Name:");
     Owner_Name = sc.nextLine();

     PrivateVehicle PrivateCar = new PrivateVehicle(Car_Number, Car_Model,
                            Manufacture_Yaer, Comment, Owner_Name);

     try{
       CarsGarage.add(PrivateCar);
     }
     catch (FullException e){
       continue;
     }

.........
.........
}

Hope the code is clear. Vehicle is the super class and it only contains some more details about the car. The Garage class suppose to hold all of the cars in a hashmap. There are two types of cars, PrivateVehicle which is mention the code and LeesingVehicle which is not, both are subclasses of Vehicle.

when I try to compile it using javac -Xlint:unchecked *.java, I get the following

Main.java:79: warning: [unchecked] unchecked call to add(T) as a member of the raw type Garage
                        CarsGarage.add(PrivateCar);
                                      ^
  where T is a type-variable:
    T extends Vehicle declared in class Garage
Main.java:97: warning: [unchecked] unchecked call to add(T) as a member of the raw type Garage
                        CarsGarage.add(LeasedCar);
                                      ^
  where T is a type-variable:
    T extends Vehicle declared in class Garage
Main.java:117: warning: [unchecked] unchecked conversion
                    CarsList = CarsGarage.getAll();
                                                ^
  required: ArrayList<Vehicle>
  found:    ArrayList
3 warnings

How can I avoid this warnings?

Thanks.

Upvotes: 3

Views: 676

Answers (1)

zw324
zw324

Reputation: 27200

Garage CarsGarage = new Garage(20);

Here you are not specifying a type parameter for Garage, which is actually a generic class Garage<T extends Vehicle>. You need:

Garage<Vehicle> CarsGarage = new Garage<Vehicle>(20);

Upvotes: 3

Related Questions