[Solved] Why when I uncommenting toString it writes null?


The reason is name and id are null.

When you retrieve this.id, it returns the value of this classes (ProductEntry) member variable rather than its super class (which is what you want in this case). If these member variables weren’t defined in ProductEntry, a call to this.id would retrieve the id from the super class, in this case Entry. This is known as Variable shadowing

You can solve the problem in two possible ways. The first is to simply remove name and id member variables from the sub classes of Entry. This will mean this.id will access the member variable of Entry rather than its sub class ProductEntry. The other option is to simply assign these in the constructor, but as you already storing these values in the super class, I don’t think this is the best approach.

In the spirit of encapsulation you also may want to consider making your member variables private (only accessible by the class there defined in), or protected (accessible by the class its defined in and its sub types).

solved Why when I uncommenting toString it writes null?