I need a method in a class that is going to be used in subclasses, although this method uses a property that is changed in subclasses. Is there a way to access the property of the subclass without having to override the method?
I've tried using a getter for the property, but got the same result.
public class SuperClass {
private static final String a = "Super";
public void superMethod(){
System.out.println("SuperMethod: " + a);
}
}
public class ChildClass extends SuperClass {
private static final String a = "Child";
}
public class Main {
public static void main(String[] args) {
SuperClass s = new SuperClass();
ChildClass c = new ChildClass();
s.superMethod();
c.superMethod();
}
}
The console shows:
SuperMethod: Super
SuperMethod: Super
The expected result is:
SuperMethod: Super
SuperMethod: Child