Access toString method from generic class [closed]

-3

I have a generic Employee class and 3 other specific classes that inherit from Employee. Within the Employee class, I have the toString method.

abstract class Funcionario {
    private String nome;
    private String documento;

    public String toString(){
         return "\nNome e Documento";
    }
}

class Motorista extends Funcionario {
    private String cnh;

    public String toString(){
        return "CNH";
    }
}

class Secretaria extends Funcionario {
    private String telefone;

    public String toString(){
        return "Telefone";
    } 
}

class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        Secretaria fulana = new Secretaria();
        System.out.println(fulana);

        Secretaria ciclana = new Secretaria();
        System.out.println(ciclana);

        Motorista beltrano = new Motorista();
        System.out.println(beltrano);
    }
}

There are other classes that also inherit from Officer . Is there a way, (maybe by polymorphism) that I can 'iterate' over all objects and execute the toString method of each?

Instead of calling one object at a time within System.out.print , is there any way to save code?

Link to Ideone Code here .

    
asked by anonymous 31.10.2018 / 21:09

1 answer

-2

So I did not quite understand the question. Iterations are made on lists, if this is what you want you should store all your employees in a List, but there is no need to iterate over all objects if the only information you need is how many instances were created, just fetch that information with the size method.

import java.util.ArrayList;

abstract class Funcionario {
    protected String nome;
    protected String documento;

    public String toString(){
        return "\n--Funcionário--" +
               "\nNome: " + this.nome +
               "\nDocumento: " + this.documento;
    }
}

class Motorista extends Funcionario{

    Motorista(String nome, String documento) {
        this.nome = nome;
        this.documento = documento;
    }

    public String toString(){
        return "\n--Motorista--" +
               "\nNome: " + this.nome +
               "\nDocumento: " + this.documento;
    }
}

class Secretaria extends Funcionario{
    private String telefone;

    Secretaria(String nome, String documento, String telefone) {
        this.nome = nome;
        this.documento = documento;
        this.telefone = telefone;
    }

    public String toString(){
        return "\n--Secretaria--" +
               "\nNome: " + this.nome +
               "\nDocumento: " + this.documento +
               "\nTelefone: " + this.telefone;
    } 
}

public class Ideone {

    public static void main(String[] args) throws java.lang.Exception {
        ArrayList<Funcionario> lista = new ArrayList();

        lista.add(new Secretaria("Fulana", "123.456.789-00", "3322-1234"));
        lista.add(new Secretaria("Ciclana", "578.367.263-30", "5854-8952"));
        lista.add(new Motorista("Beltrano", "654.786.324-54"));

        System.out.println("No momento existem " +lista.size()+ " funcionários cadastrados");

        for (Funcionario f : lista) {
            System.out.println(f);
        }
    }   
}
    
31.10.2018 / 22:43