1

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);
    }
}
Cloud
  • 938
  • 1
  • 8
  • 24
  • 4
    Static methods cannot be overridden. –  Jan 28 '20 at 18:43
  • 1
    Does this answer your question? [Can a static method be overridden in C#?](https://stackoverflow.com/questions/14828271/can-a-static-method-be-overridden-in-c) – Alexander Goldabin Jan 28 '20 at 18:45
  • I am aware that the code I wrote is illegal, I was just illustrating what I wanted to achieve. Does that mean it's not possible, not even with some workaround? I see that @ilyes gave an answer below where he instantiates the classes (ditches the use of static) in order to use virtual + override, but it got downvoted, and I'm not sure why. – Cloud Jan 28 '20 at 19:26
  • if you dont want to use regular objects, and only static members, why dont you create just separate class and pick whatever you want from other static classes? you can encapsulate invoking of other static methods (like inheritance) inside of other static methods. But this is not the road I advise to go. – Yehor Androsov Jan 28 '20 at 19:31
  • @ilyes looks like author cares too much about everything staying static... – Yehor Androsov Jan 28 '20 at 19:31
  • @pwrigshihanomoronimo could you perhaps post an answer illustrating what you've said (I don't really get it) and also explain why you'd advise against doing so? – Cloud Jan 28 '20 at 19:42
  • How about this: Rewrite the methods that use the properties so that they take the properties they need as parameters. Then instead of `ChildA.MyMethod()`, you'd do `Parent.MyMethod("A")` – Sweeper Jan 28 '20 at 19:47
  • @Sweeper I would like to have many methods in each child that make use of that same property. For more context, I am creating Data Access Objects in which each class represents a table in a relational database. So I want the table name to be reused per class. – Cloud Jan 28 '20 at 19:51
  • @Cloud That does not contradict with anything I just said. You could have all those methods all accepting an extra parameter, which is "the property that it makes use of". And when calling them, you just pass in the property you want. Note that you don't have to pass in the string literal. You could use named constants, and name them with a similar name to your classes. – Sweeper Jan 28 '20 at 19:55
  • @Sweeper Hmm, I see what you mean now. However, that would mean breaking the architectural rule of each table having its own DAO. And since I would still need methods specific to each table (eg selecting by username in a User table), I would still need table-specific DAOs. Were I to do something like that, I'd have to write `Parent.MyMethod("A")` or `Parent.MyMethod(ClassA.MyProperty)`, but still write things like `ClassA.MyMethodTwo("would also use its own 'A' MyProperty")` anyway, which could get messy. – Cloud Jan 28 '20 at 20:40

0 Answers0