I am a beginner.
Everything works ok except those two attributes where I entered the number 0. I have tried without success. Is there a way I can execute b.print(); without having to insert those 0(s)?
Does that mean you want somebody who has 0 as postcode and 0 as age? That appears strange to me. You can overload the constructor, so you have two constructors, one of which sets those two fields to some sort of default value.
If your toString() method is behaving the way you want it to, then you'd have to provide another constructor that doesn't take the last two parameters that the current constructor takes. The closest thing to optional parameters that Java provides is allowing the last parameter to be variadic. However, a variadic parameter should represent a list of related values, not two separate and unrelated values like postal code and age.
Did you know that you can have more than one constructor for a class as long as each of their argument lists differ from the others in types or number of parameters?
The best ideas are the crazy ones. If you have a crazy idea and it works, it's really valuable.—Kent Beck
When a class toString() method is going to list the value of many (or all) of its attributes, including those specified in its superclasses, it is often desirable to just call super.toString() rather than repeating all of that code.
This is especially useful if the superclass has many attributes, or if for some reason the toString() method of one of the superclasses is modified, since you then do not have to modify all of the subclass toString() methods.
also, the toString() methods often enclose the values in brackets, so you do not end up with a bunch of periods at the end of each class output:
Self destruct mode activated. Instructions for deactivation encoded in this tiny ad.