Access toString method from generic class

Asked

Viewed 223 times

-3

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

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 Functionary. Is there any way (perhaps by polymorphism) that I can 'iterate' on all objects and execute the toString method of each one?

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

Link to Code in Ideone here.

  • 2

    The code is too much played, if we were to try to execute this would give a lot of error beyond the error you are asking, could you do a [mcve] to help us help you? I even think I understand what you want, but in the current form I may end up speculating one thing and be another.

  • 2

    The example improved a lot and I saw that I was preparing in ideone what would make it easier to put the link to us forkar, but now it got weird because each of these objects has no relationship between them to make that for initial, without having something that binds all classes in some way on some object, if it is all spread in loose variables, even in the same function, the biggest problem is this and not the polymorphism. And I don’t know if the toString() have become significant enough for what you want, but it may be yes for example, I’m just speculating on that part.

  • just inside those same classes that inherit the toString() method: @Override public String toString(){ ... I hope this helps

  • If you have an array or ArrayList of Funcionario just iterate and call the toString. Isn’t that the way it is ? You have 150 variable loose employees as the example gives the idea ?

  • @Maniero I forgot to relate the elements through inheritance. But basically the idea is code reuse. Maybe I am expressing myself wrong, but basically I would like to know the easiest way to execute the 'toString' method on all objects from the generic class.

  • The problem is that your code is all about another problem that has nothing to do with what you’re talking about, and we’re saying that and you’re ignoring it. There is a conceptual problem in your use of toString(), but that’s another matter, the fact is that there is no doubt posted, you a code, it works and everything ok. So you talk about iterating, but iterating what? Loose objects? That’s not possible, nor does it make sense. First you need to understand what an iteration is. https://answall.com/q/185497/101. If you are unable to express yourself correctly you need to resolve this p/ we can help you

  • @Maniero The code comes from the real project, I just thought it best not to insert all fields and methods as they are not necessary for the case. I understand the idea of iteration, so I put the quotes. My idea is pretty much what user129943 quoted, but I didn’t want to make use of lists.

  • Unfortunately, without saying what you want, it’s hard to answer. I practically answered in the comments, but and this is not enough I do not know what the question is.

  • @Maniero Haha! I will try to be as objective as possible.. Through superclass, can I through polymorphism call the toString method of each object you inherited from Funcio? Instead of calling the Print of each object individually.

  • You keep saying nothing useful and the fact that you accept an answer that you yourself say is not good for you shows that I was wrong not to have closed the question from the beginning (acceptance turned out to be a disservice to the community). What you want doesn’t make sense and I said it from the beginning.

Show 5 more comments

1 answer

-2


Then I don’t quite understand the question. Iterations are made over lists, if that’s what you want you should store all your employees on a List, but there’s no need to iterate over all objects if the only information you need is how many instances have been created, just fetch this 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);
        }
    }   
}
  • That’s pretty much what I’m looking for, but without the use of the list, I’m interested to know how I could make use of polymorphism to access these methods through the Employee class.

  • That’s exactly what I’m doing in the code. I am saving Secretary and Driver on an Employee list (this is possible because Employee is the parent class), and when I pass this object to the println, which automatically invokes toString, the toString implemented in the daughter class is invoked. Take the test. Funcio fulana = new Secretaria(); System.out.println(fulana);

  • I didn’t want to implement a list, but after implementing the code you provided worked perfectly. The people of Stackoverflow international indicated me the use of the 'super' when invoking the toString method, and after implementing with their tip the algorithm worked perfectly.

Browser other questions tagged

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