Is there any advantage to storing a reference to a child instance field in the base class, as opposed to always calling an abstract getter when accessing it in the base class?
Suppose I have a base class like below:
public abstract class BaseClass {
abstract String getText();
public void printText() {
System.out.println(getText());
}
}
And a child class like below where getText() is returning a field that will never change.
public ChildClass extends BaseClass {
private String text = "blah";
@Override
public String getText() {
return text;
}
}
Is there any advantage/disadvantage to converting to the below instead?
public abstract class BaseClass {
abstract String getText();
private String text;
public void printText() {
System.out.println(text);
}
@PostConstruct
public void postConstruct() {
this.text = getText();
}
}