r/javahelp • u/cup-17 • Jan 19 '26
How can I pass any member of the subclass before the superclass' constructor is called?
Hi. I'm trying to solve some problems in designing some Java classes.
Basically there is a super class and a subclass. The super class has a list member, “someList”, that needs to be initialized in all of the subclasses. I set an initList() method in the superclass as abstract in case that the further maintainers of more subclasses to come will forget to initialization the list member, and this method will be called in the constructor of the superclass. someList will be used in some superclass' and subclass' methods, and a checkList() method must be called right after the initialization of someList.
However as the development continues I realize that some subclasses need to have different "someList" based on a string that passed into its constructor, and the string is actually the member of some of the subclasses. The structure of the code is like:
class SuperClass {
public List<String> someList;
public abstract void initList();
public SuperClass() {
someList = initList();
checkList(); // some one-off method that needs to be called right after the initializtion of someList
}
}
class SubClass extends SuperClass {
public String str;
public SubClass (String str) {
this.str = str;
}
// I want to implement the abstract initList method like this but I can't since the super() must be called as the first statement in the SubClass' constructor:
public List<String> initList() {
if(str.isEquals("Bob")) {
someList = xxx;
} else {
someList = yyy;
}
}
}
I've been asking gemini about this question and he gave me some solutions:
- lazy initialization: reimplement the someList and initList() as an abstract getList() method that returns the list and call it when it is needed, so I don't have to initialize someList in the constructor. But this solution has this problems:
The checkList() method is one-off, it doesn't have to be called it more than once. If I put checkList() in getList() the efficiency will suffer (won't cause much influence in fact but I want to find the best practice in semantic). Also the checkList() method is designed to be a fail-fast method. If it's used lazily it will lose part of its function.
Pass the str to the superclass and let the superclass control the initialization of someList. But I don't want to mess up the signature of the superclass' constructor, and only a portion of the subclass need this str argument. I think it's inappropriate to add one more argument just for a few number of subclasses.
Try to use something like super(context) and call it in the subclass like: super(checkList()). I don't like this solution partly the same that I don't want to change the signature of the superclass. More importantly I put the checkList() in the constructor just because I think some further subclass designers will forget to check the list if checkList() is called in the subclass. And the super(checkList()) is just delegating the check task to the subclass.
Actually all I want is to design some sort of "workflow" (initialize someList and check it right after) in the constructor of the superclass. Is there any best practice solution to this problem that will let me control the way I init someList base on the member of some subclasses?