title: Using parent class toString in object Array to print parent class variables for child class JAVA
body: <p>I am trying to write a code that takes vehicle names and such and stores them in an array of objects. I have Vehicle as the parent class with a toString that prints the name, brand and gallons of tank, the two subclasses of parent are bus and car. Car and Bus ask how many doors,wheels or passengers. When printing the array out after randomizing the order i only get the to Strings from the parent class to print out when the index of the array is of the parent class. What i want to print out is:
  Vehicle 0:
  name: car brand: something : tanksize: 15
  Doors: 4 : Wheels 4</p>
  <hr />
  <p>My code is below:</p>
  <pre><code>import java.util.*;
  
  public class Q1 {
      public static void main(String args[]){
          
          Scanner kb = new Scanner(System.in);
  
          Vehicle[] list = new Vehicle[3];
  
          for(int i =0;i &lt; list.length;i++){
              int random = (int)(Math.random()*3);
              if(random == 0){
                  System.out.println(&quot;What is the Name, brand and how big is the gas tank of the vehicle?&quot;);
                  String name = kb.next();
                  String brand = kb.next();
                  int gasTank = kb.nextInt();
                  
                  Vehicle a = new Vehicle(name,brand,gasTank);
                  
                  list[i] = a;
      
              }
              else if(random == 1){
                  System.out.println(&quot;How many doors and wheels does the car have&quot;);
                  int doors = kb.nextInt();
                  int wheels = kb.nextInt();
                  
                  System.out.println(&quot;What is the Name, brand and how big is the gas tank of the car?&quot;);
                  String name = kb.next();
                  String brand = kb.next();
                  int gasTank = kb.nextInt();
                  
                  Car a = new Car(doors,wheels,name,brand,gasTank);
                  list[i] = a;
              }
              else{
                  System.out.println(&quot;How many doors and wheels does the bus have?&quot;);
                  int doors = kb.nextInt();
                  int wheels = kb.nextInt();
                  
      
                  System.out.println(&quot;What is the Name, brand and how big is the gas tank of the bus?&quot;);
                  String name = kb.next();
                  String brand = kb.next();
                  int gasTank = kb.nextInt();
                  
                  Bus a = new Bus(doors,wheels,name,brand,gasTank);
      
                  list[i] = a;
              }
              
          }
          printArray2(list);
          
  
  
      }
      public static void printArray(Vehicle[] list){
          for(int i = 0; i&lt; list.length; i++){
              System.out.println(&quot;Vehicle &quot;+ i);
              System.out.println(list[i]);
              System.out.println(&quot;___________&quot;);
          }
      }
      public static void printArray2(Vehicle[] list){
          for(int i = 0; i&lt; list.length;i++){
              System.out.println(&quot;Vehicle &quot;+ i);
              System.out.println(list[i].toString());
              System.out.println(&quot;___________&quot;);
          }
          
          
      }
  }
  
  
  class Vehicle {
      String name;
      String brand;
      int gasTank;
  
      public Vehicle(){
          this(&quot; &quot;, &quot; &quot;, 15);
      }
      public Vehicle(String name, String brand,int gasTank){
          this.name = name;
          this.brand = brand;
          this.gasTank = gasTank;
  
      }
  
      public String getname(){
          return name;
      }
      public String getbrand(){
          return brand;
      }  
      public int getgasTank(){
          return gasTank;
      } 
      public void setname(String name){
          this.name = name;
      }
      public void setbrand(String brand){
          this.brand = brand;
      }
      public void setgasTank(int gasTank){
          this.gasTank = gasTank;
      }
      
      public double milesPerGallon(double miles, double gallonsUsed){
          return  miles / gallonsUsed;
      }
  
      public String toString(){
          return &quot; name: &quot; + name + &quot; Brand: &quot; + brand + &quot; Gallons in Tank: &quot; + gasTank;
      }
  
      public double fillTank(double gallonsUsed){
  
          return (gasTank - gallonsUsed);
      }
  
  }
  
  
  class Car extends Vehicle{
      int numOfdoors;
      int numOfWheels;
  
      public Car(){
          this(2,4,&quot;&quot;,&quot;&quot;,10);
      }
      public Car(int numOfDoors, int numOfWheels, String name, String brand, int gasTank){
          this.numOfdoors = numOfDoors;
          this.numOfWheels = numOfWheels;
          super.name = name;
          super.brand = brand;
          super.gasTank = gasTank;
      }
      public int getnumOfdoors(){
          return numOfdoors;
      }
  
      public int getnumOfWheels(){
          return numOfWheels;
      }
      public void setnumOfdoors(int numOfdoors){
          this.numOfdoors = numOfdoors;
      }
      public double milesPerGallon(double miles, double gallonsUsed){
          return miles / gallonsUsed;
      }
  
      public String toString(){
          return &quot;The car has &quot; + numOfWheels + &quot; wheels and &quot; + numOfdoors + &quot; doors&quot;;
      }
  }
  
  class Bus extends Vehicle {
      int numOfWheels;
      int numOfPassengers;
  
      public Bus(){
          this(1,8,&quot;&quot;,&quot;&quot;, 50);
      }
      public Bus(int numOfWheels, int numOfPassengers, String name, String brand, int gasTank){
          this.numOfWheels = numOfWheels;
          this.numOfPassengers = numOfPassengers;
          super.name = name;
          super.brand = brand;
          super.gasTank = gasTank;
      }
  
      public int getNumOfWheels(){
          return numOfWheels;
      }
      public int getNumOfPassengers(){
          return numOfPassengers;
      }
  
      public void setNumOfWheels(int numOfWheels){
          this.numOfWheels = numOfWheels;
      }
      public void setNumOfPassengers(int numOfPassengers){
          this.numOfPassengers = numOfPassengers;
      }
      public double milesPerGallon(double miles, double gallonsUsed){
          return miles / gallonsUsed;
      }
      public String toString(){
          return &quot;The bus has &quot;+ numOfWheels + &quot; Wheels and &quot; + numOfPassengers + &quot; Passengers&quot;;
      }
  }
  </code></pre>