Inheritance in Java regarding multiple classes

934 Views Asked by At

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.

4

There are 4 best solutions below

4
On BEST ANSWER

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.

0
On

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
    }
}
0
On

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.

0
On

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.