0

I have a class called father that has List vehicles as its variable;

import java.util.List;

public class Father {

    String name;
    String weight;
    String age;
    String height;
    String country;
    List<Vehicle> vehicles;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getWeight() {
        return weight;
    }
    public void setWeight(String weight) {
        this.weight = weight;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    public String getHeight() {
        return height;
    }
    public void setHeight(String height) {
        this.height = height;
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
    public List<Vehicle> getVehicles() {
        return vehicles;
    }
    public void setVehicles(List<Vehicle> vehicles) {
        this.vehicles = vehicles;
    }

}

I also have another class called Vehicle

public class Vehicle {
    String brand;
    String type;

    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
}

I want to get Father's vehicle brand inside a main class.How do I do that? For example i want to print father's vehicle brand,add new vehicle brand and print the last vehicle brand that father has.

This is what I have tried inside a main class

    import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Vehicle> vehicleList = new ArrayList<>();

        Vehicle vehicle = new Vehicle();
        vehicle.setBrand("Toyota");
        vehicleList.add(vehicle);
        
        Father father = new Father();
        father.setVehicles(vehicleList);

        System.out.println(father.getVehicles());
        
    }
}

The result that i get is [Vehicle@15db9742]

Brandon Buck
  • 7,177
  • 2
  • 30
  • 51
  • 1
    *" want to get Father's vehicle brand"* -- this may not exist. Since a Father can have multiple Vehicles (the class has a List, not a single field), there is no one single car brand that you can be guaranteed to get. You can get the List via `getVehicles()` iterate through it and extract all the brands of the Vehicles it holds (including none, if there are no vehicles added), and you can add a new Vehicle to the list. That's it. – Hovercraft Full Of Eels Sep 06 '22 at 20:15
  • Write methods on the `Father` class to do these things and then call these methods. – Brandon Buck Sep 06 '22 at 20:15
  • 1
    Please look at [How do I print my Java object without getting “SomeType@2f92e0f4”?](http://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4), and then give Vehicle and Father a decent `@Override public String toString()` method. – Hovercraft Full Of Eels Sep 06 '22 at 20:27

0 Answers0