Overriding a variable type in a subclass

Hey there!

Let’s say I have:

public class ParentClass : MonoBehaviour
protected ParentObject _myTemplate;

public class SubClass : ParentClass         //the class I want to focus on

public class ParentObject : ScriptableObject
public float FloatValue;

public class SubObject : ParentObject
public bool BoolValue;

I have in my ParentClass a variable of type ParentObject. I would like to override it in my SubClass with a variable of type SubObject.

So to summerize, I want some kind of a :

public class SubClass : ParentClass
_movementTemplate => _movementTemplate as SO_CharacterMovementTemplate;

Is there a way to do something like this? Thanks in advance!

A few options:

  1. You could make the type generic and then specify it as a type argument to the subclass.
  2. You could make the type less derived on the base class then down cast to a more derived class ad hoc.
  3. You could make it dynamic (not advised)

Out of the three I would probably favour the use of generics

That said I would question if inheritance is even something you should be doing here. What is the actual problem you are trying to solve and why do you think inheritance is the way to solve it?

The first option is the one that I picked, thank you very much for your answer, I haven’t thought of that solution at all!

Actually the case in which I wanted this solution is kinda obsolete, the system changed. But I really wanted to know if this kind of thing was possible or not.

Anyway, thank you again!