dimanche 24 octobre 2021

Check if class contains / is part of another parent class

I would like to check whether a class is a subclass (note: Not correct term! I am not talking about inheritance, but rather about a class being a member of another class) with regard to the implementation.

Example

@NoArgsConstructor
@AllArgsConstructor
public class Car  {

    private String id;
    private String brand;
    private Vehicle vehicle;
}

@NoArgsConstructor
@AllArgsConstructor
public class Vehicle{

    private String id;
    private String brand;
    private Rims rims;
}

@NoArgsConstructor
@AllArgsConstructor
public class Rims {

    private String id;
    private String brand;
}

You can see here three classes with an association (A car has a vehicle, a vehicle has a rim..)

Let's assume that I can extract a Rim with properties

Car car = new Car(123, "Mercedes", 4)  *id=123, brand="mercedes", rims=4*
Vehicle vehicle = new Vehicle(123, 'michellin', 4)
Rims rims = new Rims(333, 'amg')

Now I want to check whether the rims with the id of '333' is a subclass of car with the id of 123 => This is why I talked about is subclass of the implementation .

Is there a way to do this? instanceOf and isAssignable do not work.

id: 123 and brand: michellin, and I want to check whether it's a subclass.





Aucun commentaire:

Enregistrer un commentaire