I have the following problem. I want one broad abstract type called MessageField. The run-time use of the MessageField is to carry around a String value; the rest of the type should be a bunch of constant definitions and behavior. Each subclass of MessageField should be a set of constant final members that define the type and, if necessary, can also override other behavioral methods.
So for example:
public abstract class MessageField {
//whatever needs to go here to make this work
//methods that define behavior
//e.g. public int getFieldId() { return fieldId; }
}
public class TypeOne extends MessageField {
public final int fieldId = 1; //type defining member
public final String tag = "->"; //type-defining member
public String fieldValue; //freely settable/gettable
//change any behavior if necessary
}
public class TypeTwo extends MessageField {
public final int fieldId = 2;
public final String tag = "++";
public String fieldValue;
}
public class TypeThree extends MessageField {
public final int fieldId = 3;
public final String tag = "blah";
public String fieldValue;
}
Each subclass should be forced to define a value for fieldId and tag when it's written. I guess conceptually these are static final constants for each type, but how does that work with inheritance? I need all the various types to conform to one broad overarching type which can be passed around.
Maybe inheritance isn't the best way to accomplish this pattern, but if not, what is? And if inheritance is the way to do this, how would it be done?
Thanks!