Is it possible to have some sort of static abstract property in a parent class that the child class then overrides, such that the parent is then able to make use of this now-initialised property in its methods?
I know this is illegal code, but it should illustrate what I am trying to achieve:
public class Parent {
public static abstract String PROPERTY;
public static String MyMethod() {
return $"Do something with {PROPERTY}";
}
public static String MyMethodTwo() {
return $"Do something with the same property {PROPERTY}";
}
}
public class ChildA {
public static override String PROPERTY = "A";
}
public class ChildB {
public static override String PROPERTY = "B";
}
ChildA.MyMethod() //expecting "Do something with A"
ChildB.MyMethod() //expecting "Do something with B"
Right now, I am doing something like this, which works, but can get repetitive when I'm making many parent methods and child classes:
public class Parent {
protected static String MyMethodBase(String property) {
return $"Do something with {MyProperty}";
}
protected static String MyMethodTwoBase(String property) {
return $"Do something with the same property {MyProperty}";
}
}
public class ChildA {
public static String PROPERTY = "A";
public static String MyMethod(){
return MyMethodBase(PROPERTY);
}
public static String MyMethodTwo(){
return MyMethodTwoBase(PROPERTY);
}
}
public class ChildB {
public static String PROPERTY = "B";
public static String MyMethod(){
return MyMethodBase(PROPERTY);
}
public static String MyMethodTwo(){
return MyMethodTwoBase(PROPERTY);
}
}