I need to program a parent class (Plate) and some child classes (WoodenPlate, PorcelainPlate, etc) each of the Child Objects should have a serial number starting with 1, then 2, etc.
Where should I store this information? Can I just increment a counter in the Parent Object?
I need to program a parent class (Plate) and some child classes (WoodenPlate, PorcelainPlate,
Share
This is how your one of your
Childclass should look like:And then for each instance of
WoodenPlate, you can access theserialNumberusing public accessorsYou can define rest of your
Childclasses like this. With each one of them having astaticcounter and aserialNumberfield.If you don’t want the count to start from
1fro everyChild, but just want the count to continue from the previous one regardless of which Child class was instantiated, then you should have those fields inParentclass:And then remove the field from each of your
Childclass.