-
Counter
Hello everyone,
I have this project for homework where I need to create one super class(Solids) and three subclasses (Cylinder, Cone and Sphere). The exercise says "... there will be a counter for each type of solid, so that we know how many have been created (of each type)...". My question is, is there a way that I can create the counter ONCE and then use it for all the subclasses? If so, how do I do that? (NOTE: I am familiar with the homework policy and I am not asking for the code, just a hint or something).
Thank you
-
Re: Counter
In each subclass declare a static variable counter.
Increment it in the constructor itself
for e.g
class Cylinder extends Solids{
static int counter1;
Cylinder(){
counter1=counter1+1;
}
-
Re: Counter