Home > Mobile >  Accessing properties of Generic class in Java
Accessing properties of Generic class in Java

Time:04-19

Hi I am new to Java Generics and I am trying to access properties inside generic class. So far I have found C# related answers, it would be of great help if someone could please guide as to how I should do it in java. Say I have Class Car and it has properties integer type wheels and String type model. Now I have a method say vehiclehelper(List<T> vehicles) which takes a generic type list. At place of invocation of vehiclehelper, it will be like :

List<Car> cars = new ArrayList<>();
cars.add(new Car(4, "Honda"));
vehiclehelper(cars);

Now coming to vehiclehelpermethod, in its body I want to loop over the list<T>vehicles and fetch the wheels property from it..something like:

for(T vehicle: vehicles)
  count  = vehicle.getWheels();

Now here at this point I am getting error, saying property isn't defined. What should I do to fetch the wheel property from my list of generic type?

CodePudding user response:

create an interface ICar or an abstract class that represents a car, and has the methods that you expect a car to have, (e.g getWheels()).

then send a list of the objects that implements/extend this class as a parameter carHelper(List<ICar> cars)

CodePudding user response:

Your class Car is not generalized. It's common class. You just manipulate Car objects via generalized List collection. It should not any issues with access to Car fields through getters or setters if that fields are private. Code below works perfectly.

public class App {
    public static void main(String[] args) {
        List<Car> carList = new ArrayList<>();
        carList.add(new Car(4, "Honda"));
        carList.add(new Car(3, "Polaris Slingshot"));
        System.out.println(carHelper(carList)); // the result is 7
    }

    static int carHelper(List<Car> cars) {
        int count = 0;
        for (Car car : cars) {
            count  = car.getWheel();
        }
        return count;
    }
}

class Car {
    private int wheels;
    private String name;

    public Car(int wheels, String name) {
        this.wheels = wheels;
        this.name = name;
    }

    public int getWheels() {
        return wheels;
    }

    public void setWheels(int wheels) {
        this.wheels = wheels;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

If you want to have universal method carHelper for any object type, then you have to make it generalized:

static <T> int carHelper(List<T> elements) {
    int count = 0;
    for (T element : elements) {
        count  = element.getWheel();
    }
    return count;
}

But in the case you will get compile-time error at element.getWheel() because T type is some unknown type. Compiler does not know about his methods. To find some generic solution we have to define boundary for method parameters. Let's say input parameters will be Vehicle or it's children. In Vehicle class, let's say, we will introduce needed fields and his descendants will inherit them. In the case below listed method will work. Read about wildcards in generics.

static  int carHelper(List<? extends Car> elements) {
    int count = 0;
    for (Car element : elements) {
        count  = element.getWheels();
    }
    return count;
}
  • Related