0

Suppose I have a parent class called MotorVehicle and it has some data members. Then I have two more sub classes called Car, and Truck that extends the parent class MotorVehicle.

Now, in my main method, I have an ArrayList of type MotorVehicle

ArrayList<MotorVehicle> myList = new ArrayList<MotorVehicle>();

Now, my main method is to use methods to ADD, REMOVE, LIST, SAVE, SORT objects from all four classes in an ArrayList of type MotorVehicle.

Will I be able to add an object of type CAR to this list? I am really confused. Below is a sample input (and the object that is created is stored in the arrayList):

Enter what you want to do-ADD,REMOVE,LIST,SAVE,SORT: aDd Enter vehicle type- CAR, BUS, TRUCK, OTHER: CaR
Enter the number of wheels: 4
Enter the engine size in cc: 300
Enter true for power steering else false: false
Enter the purchase date as mm/dd/yyyy: 11/22/2002 Enter the vehicle serial number: ADT13478
Enter the number of doors: 2
Enter the car's color: White

The above series of questions create an object of type CAR, and is stored in myList. How does that work? myList isn't of type Car.

I am really confused.

G.K.
  • 33
  • 8

4 Answers4

1

That's the way how it behave. You created a list of ArrayList<MotorVehicle>. Car is a subclass of MotorVehicle therefore when you add a Car object into a MotorVehicle list is totally okay.

For example, you can do the following

final Car my car = new Car();
if (car instanceof MotorVehicle) { System.out.println("I am a motor vehicle"); }
if (car instanceof Car) { System.out.println("I am a car"); }

A car is a Car and also a MotorVehicle.

beyonddc
  • 1,246
  • 3
  • 13
  • 26
1

Yes.

As Car has MotorVehicle as a parent it will inherit it's methods unless overridden within your declaration of Car. myList isn't of type Car but it is of type MotorVehicle and since Car is a type of MotorVehicle it could be used.

Conversely you could not say ArrayList<Car> myList = new ArrayList<Car>(); and then add a MotorVehicle to the list because some types of MotorVehicle aren't Car. All cars are motor vehicles but not all motor vehicles are cars. So if you are grouping motor vehicles then cars can be part of that.

It's the same idea as if you had a parent named Fruit and you had Apples and Oranges as subclasses. They are both Fruit and may have a colour, taste etc... but if you have an array of Apples then an Orange cannot belong.

Moiety Design
  • 473
  • 4
  • 9
  • Extending the stream of confusion. The car class doesn't have a field for serial number, but the MotorVehicle class does. Now, when creating an object for type `Car` we are asked to enter a `serialNumber` that is of type `MotorVehicle`. How do I achieve this? Add the `Car` object and with it the serialNumber field for MotorVehicle? – G.K. Nov 12 '14 at 18:13
  • Just by declaring it. `Car` extends `MotorVehicle` so you can declare that field straight out. Any properties declared in `MotorVehicle` will carry over to `Car` and can be just used directly. If you declare the field in the subclass `Car` it will replace the properties however. So if you had `int serialNumber` in `MotorVehicle` you have to use `int serialNumber` in `Car` if not declared but if in `Car` you declare `String serialNumber` then you can use strings for any `Car` because it has had the integer requirement written over for just `Car` cases. – Moiety Design Nov 16 '14 at 22:09
  • I figured that out. Our professor had made the mistake and not specified the data member in the UML Diagram. I finished the code, but now I am facing another issue. All of these Car, Bus, and Truck are a sub type of MotorVechile. Whenever I add a new instance of car or bus or truck to the motorVehicle array all the elements' motorVechile data members are overwritten with the new one's value. Any idea where I am going wrong? For ever object of car or bus or truck I am making a new object of motorVehicle in their class and then calling the constructor for MotorVehicle from there. Any thoughts? – G.K. Nov 17 '14 at 13:38
  • Are you making sure that the data members are applied to the objects themselves rather than the set. So in the constructors of `Car`, `Bus`, `Truck` have `this.property = property` or whatever variation on that. Otherwise you are modifying the total properties of the set rather than for the individual objects. – Moiety Design Nov 17 '14 at 22:15
1

Since you defined Car to extend MotorVehicle, it is a MotorVehicle only a particular type of it, so you can add it to a list of MotorVehicles. When you want to do something with objects in the list you might need to check what specific type they are depending on what it is you want to do. You can do that with instanceof, see this for more information.

Community
  • 1
  • 1
benji
  • 606
  • 6
  • 12
1

This example will help you understand how its working

class AClass {
    public void printA() {
        System.out.println("Inside AClass.printA");
    }
}

class BClass extends AClass {
    @Override
    public void printA() {
        System.out.println("Inside BClass.printA");
    }
    public void printB() {
        System.out.println("Inside BClass.printB");
    }

    public static void main(String[] args) {
        ArrayList<AClass> list = new ArrayList<AClass>();
        list.add(new BClass());

        list.get(0).printA();
        list.get(0).printB(); //Compile time error "The method printB() is undefined for the type AClass"

        ((BClass) list.get(0)).printB(); //After casting it will work
    }
}
Naman Gala
  • 4,670
  • 1
  • 21
  • 55