-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.
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.
– Maniero
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 thetoString()
have become significant enough for what you want, but it may be yes for example, I’m just speculating on that part.– Maniero
just inside those same classes that inherit the toString() method: @Override public String toString(){ ... I hope this helps
– jpcnt
If you have an array or
ArrayList
ofFuncionario
just iterate and call thetoString
. Isn’t that the way it is ? You have 150 variable loose employees as the example gives the idea ?– Isac
@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.
– Johnnes Souza
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
@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.
– Johnnes Souza
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
@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.
– Johnnes Souza
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.
– Maniero