I have an abstract class that has a implements a abstract method, that works as a placeholder for the getters of a certain value in its childs:
public abstract class MyAbstractClass {
public abstract int GetMyValue();
}
public class MyChildClass0 : MyAbstractClass {
int myValue = 1;
public override int GetMyValue {
return myValue;
}
}
public class MyChildClass1 : MyAbstractClass {
int myValue = 7;
public override int GetMyValue {
return myValue;
}
As you can see, the getter behaves the same way in every child. My question is if there is any way to make that getter a non-abstract method, so I just have to implement it once in the abstract class. And everytime I want to access myValue in any child class, I just call the GetMyValue method that it inherits from MyAbstractClass.
I don't know how to do it because I don't know how to make the getter access that field, since it is not declared in the abstract class, but in the childs.
Properties and a default constructor are a great thing here.
This forces all derived classes to provide the value to
MyAbstractClass.