Cast problem

Asked

Viewed 44 times

0

I have the following classes:

public class Hotran {

    @Getter
    @Setter
    private String codEmpresa;

    @Getter
    @Setter
    private String nomeEmpresa;

    @Getter
    @Setter
    private Long voo;

    @Getter
    @Setter
    private String equipamento;

    @Getter
    @Setter
    private List<String> diasDeVoo;

    @Getter
    @Setter
    private Long quantidadeAssentos;

    @Getter
    @Setter
    private String numeroHotran;

    @Getter
    @Setter
    private Date dataSolicitacao;

    @Getter
    @Setter
    private Date dataVigencia;

    @Getter
    @Setter
    private String naturezaOperacao;

    @Getter
    @Setter
    private Long numEtapa;

    @Getter
    @Setter
    private String codOrigem;

    @Getter
    @Setter
    private String aeroOrigem;

    @Getter
    @Setter
    private String codDestino;

    @Getter
    @Setter
    private String aeroDestino;

    @Getter
    @Setter
    private Date horaPartida;

    @Getter
    @Setter
    private Date horaChegada;

    @Getter
    @Setter
    private String equipAlterado;

    public Hotran() {
        this.diasDeVoo = new ArrayList<String>();
    }

With the subclass

public class HotranComParecer extends Hotran{

    @Getter
    @Setter
    private String hotranAnt;

    @Getter
    @Setter
    private String cgna;

    @Getter
    @Setter
    private String infraero;

    @Getter
    @Setter
    private String gatr;

    @Getter
    @Setter
    private String ggco;

    @Getter
    @Setter
    private String ggfs;

    @Getter
    @Setter
    private String ggip;

    @Getter
    @Setter
    private String ggof;

    @Getter
    @Setter
    private String ggta;

    @Getter
    @Setter
    private String gopi;

    @Getter
    @Setter
    private String gopd;

    @Getter
    @Setter
    private String consolidacao;

I have the following method:

private Hotran toBasicos(Row row) {
        Hotran hotran = new Hotran();
        for (Cell cell : row) {
            switch (cell.getColumnIndex()) {
            case 0:
                hotran.setCodEmpresa(PlanilhaUtil.toString(cell));
                break;
            case 1:
                hotran.setNomeEmpresa(PlanilhaUtil.toString(cell));
                break;
            case 2:
                hotran.setVoo(PlanilhaUtil.toLong(cell));
                break;
            case 3:
                hotran.setEquipamento(PlanilhaUtil.toString(cell));
                break;
            case 4:
                hotran.getDiasDeVoo().add(PlanilhaUtil.toString(cell));
                break;
            case 5:
                hotran.getDiasDeVoo().add(PlanilhaUtil.toString(cell));
                break;
            case 6:
                hotran.getDiasDeVoo().add(PlanilhaUtil.toString(cell));
                break;
            case 7:
                hotran.getDiasDeVoo().add(PlanilhaUtil.toString(cell));
                break;
            case 8:
                hotran.getDiasDeVoo().add(PlanilhaUtil.toString(cell));
                break;
            case 9:
                hotran.getDiasDeVoo().add(PlanilhaUtil.toString(cell));
                break;
            case 10:
                hotran.getDiasDeVoo().add(PlanilhaUtil.toString(cell));
                break;
            case 11:
                hotran.setQuantidadeAssentos(PlanilhaUtil.toLong(cell));
                break;
            case 12:
                hotran.setNumeroHotran(PlanilhaUtil.toString(cell));
                break;
            }
        }
        return hotran;
    }

While trying to make:

HotranComParecer hotran = (HotranComParecer) toBasicos(row);

I have the following mistake:

java.lang.ClassCastException: com.roknauta.alure.model.Hotran cannot be cast to com.roknauta.alure.model.HotranComParecer

Can someone please help me ?

  • 1

    HotranComParecer is subclass of Hotran. You are trying to cast from a superclass (Hotran) to a subclass (Hotrancomparecer), and that is not possible. Simply explaining: "A son inherits all the behavior of the father, but the father may not possess all the behavior of the son".

  • @Articuno me is confusing this. Since Hotrancomparecer is son he can not receive the father who is Hotran ?

  • No. Reread my sentence. You cannot convert the "higher" class to the "lower" class, because the "lower" class may have behaviors that the "higher" has no idea, which makes the cast impossible.

  • @Articuno now made sense, have any hint how I can get around this ? I did so because I will use 3 more daughters and wanted to enjoy this block.

  • @I managed to create a void method.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.