user4261932
user4261932

Reputation: 45

Inheritance and arraylist in java

I am trying to create a java program where I have a parent class MotorVehicle and many different sub classes namely Car, Bus, and Truck. Now, MotorVehicle has 6 data members and each of the other classes have 2 different data members each.

In my main method, I created an ArrayList<MotorVehicle> myArrayList and I create objects of Car, Bus, and Truckand add them to this arraylist. So for example, for car I do:

Car newCar = new Car (all, the, parameters, common, and, class, specific);
myArrayList.add(newCar);

Similarly for Bus, and for Truck. During execution, I saw that the new instances or entries into the arraylist were overwriting the MotorVehicle parameters for the existing items. So for example: If I have an exisitng Car instance in the list. And then I add a Bus instance, the 6 data members of the Bus instance pertaining to the MotorVehicle class overwrite the 6 data members of the existing Car instance pertaining to the MotorVehicle class.

I thought it might get fixed by doing this (but sadly, it did not):

MotorVehicle newCar = new Car (all, the, parameters, common, and, class, specific);

That is by changing the type to MotorVehicle

Also in the respective classes, I have a constructor that is defined as following: Car for example:

public Car(all, the, parameters, common, and, class, specific){
    super(all the 6 data members of the MotorVehicle class);
    //Set the local variables - specific to Car
}

Any idea where I might be going wrong?

CAR.java : the car class

public class Car extends MotorVehicle{
private int numberDoors;
private String color;

public Car(){

}

public Car(int numberDoors, String color){
    setNumberDoors(numberDoors);
    setCarColor(color);
}

/*
Need to add date
*/
public Car(String vehicleType, int numberWheels, double engineSize, boolean powerSteering, String purchaseDate, int numberDoors, String color, String serialNumber){        
    super(vehicleType, numberWheels, engineSize, powerSteering, purchaseDate, serialNumber);
    setNumberDoors(numberDoors);
    setCarColor(color);
}

private void setNumberDoors(int numberDoors) {
    this.numberDoors = numberDoors;
}

public int getNumberDoors(){
    return numberDoors;
}

private void setCarColor(String color) {
    this.color = color;
}

private String getCarColor(){
    return color;
}

public String toString(){
    //Does printing
}
}

MotorVehicle.java : the parent class

 public  class MotorVehicle {
    static private String vehicleType;
    static private int numberWheels;
    static private double engineSize;
    static private boolean powerSteering;
    static private String purchaseDate;
        static private String serialNumber;


    //null constructor
     public MotorVehicle()
     {

     }
         //constructor with single parameter of type string
     public MotorVehicle ( String vehicleType)
     {
         setVehicleType(vehicleType);
     }
         //constructor with five parameters of type string, integer, double, boolean
     public MotorVehicle(String vehicleType, int numberWheels,double engineSize,boolean powerSteering, String
             purchaseDate, String serialNumber)
     {
         setVehicleType(vehicleType);
         setNumberWheels(numberWheels);
         setEngineSize(engineSize);
         setPowerSteering(powerSteering);
         setPurchaseDate(purchaseDate);
                 setSerialNumber(serialNumber);
     }
    public void setVehicleType (String vehicleType )
     {
         this.vehicleType=vehicleType;
     }
     static public String getVehicleType ()
     {
         return vehicleType;
     }

     public void setNumberWheels (int numberWheels)
     {
         this.numberWheels=numberWheels;
     }

     static public int getNumberWheels()
     {
         return numberWheels;
     }

     public void setEngineSize( double engineSize)
     {
         this.engineSize=engineSize;
     }

     static public double getEngineSize( )
     {
         return engineSize;

     }
     public void setPowerSteering (boolean powerSteering)
     {
         this.powerSteering=powerSteering;
     }

     static public boolean getPowerSteering()
     {
         return powerSteering;
     }

     public void setPurchaseDate (String purchaseDate )
     {
         this.purchaseDate=purchaseDate;
     }

     static public String getPurchaseDate ()
     {
         return purchaseDate; 

     }

         public void setSerialNumber (String serialNumber)
     {
         this.serialNumber=serialNumber;
     }

     static public String getSerialNumber()
     {
return serialNumber;
     }

        @Override
        public String toString() {
//Does printing
    }
    }

Add function : in the main class

add(ArrayList<MotorVehicle> myArrayList)
{
    System.out.print("Enter vehicle type- CAR, BUS, TRUCK, OTHER: ");
    type = sc.next();
    if (type.equalsIgnoreCase("Car")) {
        int wheels = 0, doors = 0;
        String date = "", color = "", serialNo = "";
        boolean powerSteering = false;
        double eSize = 0.0;

        System.out.print("Enter the number of wheels: ");
        wheels = sc.nextInt();

        System.out.print("Enter the enginer size in cc: ");
        eSize = sc.nextDouble();

        System.out.print("Enter true for power steering else false: ");
        powerSteering = sc.nextBoolean();

        System.out.print("Enter the purchase date as mm/dd/yyyy: ");
        date = sc.next();

        System.out.print("Enter the vehicle serial number: ");
        serialNo = sc.next();

        System.out.print("Enter the number of doors: ");
        doors = sc.nextInt();

        System.out.print("Enter the car's color: ");
        color = sc.next();

        MotorVehicle newCar = new Car("Car", wheels, eSize, powerSteering, date, doors, color, serialNo);
        myArrayList.add(newCar);
}

Upvotes: 0

Views: 3695

Answers (1)

brlaranjeira
brlaranjeira

Reputation: 367

The problem is that the fields are static, as you have commented. When you have a static variable in a class, it has the same value for all instances. For example, if you have Car c1 and Car c2 and set c1.field1 = "foo", the value of field1 will be "foo" for both c1 and c2. So, what you must do is make these fields non static.

Upvotes: 2

Related Questions