Problem that I have with inheritance in Java

public class GestionRepartoLocal {  
    private ArrayList<Moto> motosDisponibles;
    private ArrayList<Furgoneta> furgonetasDisponibles;

    private ArrayList<Pedido> pedidosEsperandoMoto;
    private ArrayList<Pedido> pedidosEsperandoFurgoneta;

    //Con sus respectivos setter y getter para las listas

    public void add(Transporte transporte){
    public void notificarEntregaPedido(Pedido pedido) { 
        motosDisponibles.add(motosDisponibles.size(), pedido.getTransporte());


I can not find a way to find out if the transport type object (obtained from "order") is a Moto or Van class object. These last two are daughter classes of the transport class.

public class Transporte {

    private String codigo;
    private Mapa mapa;

    public Transporte(String codTransporte, Mapa map) {
        this.codigo = codTransporte;
        this.mapa = map;


    public double coste(String posDestino){
        double costeTransporte = mapa.distancia(codigo, posDestino);
        return costeTransporte;

    public double coste(String cod1, String cod2) {
        double costeTransporte = mapa.distancia(cod1, cod2);
        return costeTransporte;

    public String getCodigo(){
        return codigo;

    public Mapa getMapa() {
        return mapa;

Thanks in advance to whoever answers. I am new writing in the forum, but not for that, I did not know the page. On previous occasions to answer questions and that were asked / answered by other users that helped me a lot.

asked by Gisel 01.05.2018 в 20:40

1 answer


Java has the reserved word instanceof . If you have something like

class Transporte {...}

class Motocicleta extends Transporte {...}

Then you can do something like

if (miObjeto instanceof Motocicleta) {
    Motocicleta m = (Motocicleta)miObjeto;

This operator is not only true with the class you used the constructor, it is also true with any parent class:

Transporte m= new Motocicleta();
boolean b= (m instanceof Transporte) && (m instanceof Motocicleta); //true
answered by 01.05.2018 / 22:02