public class Atribut {
int classid;
@Override public String toString() {
return Integer.toString(classid);
}
}
I have made this class which overrides method toString(). I plan on making many subclasses with different classid. The problem is I dont know how to set the variable classid to work in toString method.
public class cas extends Atribut{
int classid=2;
}
The problem is if I make an cas object and toString method it returns “0” not “2”.??
Much as @java_mouse recommended, just use the parent class’s variable.
Set
classid‘s value in the constructor and then you can use the superclass’stoString()just fine.