There's a few problems here. First, why is your
Main
class extending the
Livro
class? Typically the class containing the
main()
method shouldn't really extend anything.
Second, and a by-product of the first point, you've overridden the
toString()
method in the
Main
class,
not in the
Livro
class (from what I can tell). So in your code you're creating instances of
Livro
:
Livro l1 = new Livro();
But I hazard a guess that you've not overridden the
toString()
method in that class. You need to move the
toString()
method into the
Livro
class (and also annotate it with
@Override
, many IDEs will wag their fingers at you for not annotating it). For example:
public class Livro {
... your getters and setters ...
@Override
public String toString() {
return "....";
}
}
Do be aware that you need to define/override methods inside the classes that you're using, you can't just put the
toString()
method anywhere and expect it to work :)