1

I am trying to create a simple GUI with Java. I have an Arraylist executed like this:

private List<Vehicle> vehicles = new Arraylist<>();

Im trying to pass in a subclass object with more parameters than the superclass.

I have tried Casting it and overriding toString method but nothing seemed to work

My supeprclass:

public abstract class Vehicle {
    private int speed; 
    public Vehicle (int speed) {
        this.speed = speed;
    }
}

My subclasses:

public class Car extends Vehicle {
    private int gear; 
    public Car (int speed, int gear) {
        super(speed);
        this.gear = gear;
    }
}

public class Bike extends Vehicle {
    public Bike (int speed) {
        super(speed);
    }
}

I instantiate in the class where i initialize the Arraylist

Car car = new Car (100, 5);
vehicles.add(car);

i can initialize Car without problems. Parameters get sent, but the result im getting is only an instance of the superclass. Where am i going wrong?

matijap
  • 59
  • 9
  • Where is the code part with the instantiation? – Laguh May 12 '19 at 10:44
  • 1
    If you do `Vehicle v1 = new Car(100, 5)` it's obviously just a Vehicle, but you could cast it to a car if you know it's a `Car`. If you do `Car v1 = new Car(100, 5)` it's a new object of type `Car`, no need for casting. What was the question? – UninformedUser May 12 '19 at 10:47
  • 1
    remove the extra parenthesis from the class declarations – Reimeus May 12 '19 at 10:49
  • Does this code compile with the `extends ... ` **()** - I doubt it ? – UninformedUser May 12 '19 at 10:51
  • I fixed my typing errors. sorry about those. – matijap May 12 '19 at 10:53
  • You have declared your list to hold Vehicle objects so that’s why all objects you get back from the list are typed as Vehicle but in reality they have not changed. A Car is still a Car etc. Use instanceof and type casting to get the right type back for you list objects – Joakim Danielson May 12 '19 at 10:56
  • @JoakimDanielson i try to add an instance of Car to the arraylist but i get an instance of Vehicle when i print it out – matijap May 12 '19 at 10:56
  • I get that now, see my previous comment – Joakim Danielson May 12 '19 at 10:57
  • It would be helpful if you provide the complete relevant parts of the code. Instantiation? toString()? Which unexpected results where in the sources? – Christian H. Kuhn May 12 '19 at 10:58
  • Have you made sure that the `Car` class has a different `toString` method than `Vehicle`, that prints both the speed and the gear? – RealSkeptic May 12 '19 at 11:00
  • @Abra i didn’t mention anything about generics, I just tried to write an easy to to understand explanation about using sub classes in a collection defined to hold the superclass. Don’t put words in other people’s mouths and try to relax a bit. – Joakim Danielson May 12 '19 at 12:33

1 Answers1

2

It is a well-known aspect of java generics that List<Car> is not a subclass of List<Vehicle>. That's why there are constructs such as List<? extends Vehicle> and List<? super Car>. There are many online resources that show how to handle your situation. For example Generics, Inheritance and Subtypes

Abra
  • 19,142
  • 7
  • 29
  • 41