Correct syntax to access child class from parent class?

I am using inheritance and I have a parent class and a child class:

public class parentClass : MonoBehaviour
{

void Update(){

Debug.Log( childvar ); // trying to pull a field from the child class doesn't work

}

}

public class childClass : parentClass
{
public int childvar = 0;

}

of course I could do:

public class parentClass : MonoBehaviour
{

public childClass childref;//assign in inspector

void Update(){

Debug.Log( childref.childvar ); // this works

}

}

public class childClass : parentClass
{
public int childvar = 0;

}

but is there a better way?

A child is always also of the type of its parent, thus you can access the parent functionality, but the other way around simply makes no sense. Generally speaking, requiring to do this is probably related to some architectural design flaw.

The base class is a generalization and the child class a specialization. It makes sense that a specialization knows about the general concepts involved, but it does not make sense for a generalization to have access to specifics (because why use inheritance then?).
Or in other words, an Animal does not need to know any of the features a Bird specifically may have.

1 Like

childvar isn’t declared in the parent class, so it can’t know about the child’s attributes. That’s like trying to determine what the child’s wearing before it’s born.

Whenever you try top access a child class’ attribute from a parent there is a 99% chance that your logic is flawed. What are you trying to solve?

for example in my parent class I have in update something like:

public class parentClass : MonoBehaviour
{
void Update(){
//some movement code valid for all parents and child

//SOME ANIMATION CODE ONLY VALID FOR CHILD

//some cleanup code valid for all parents and child
}

so i can’t cleanly override the update in the child to be

public class childClass : parentClass
{
override void Update(){
base.Update()
//CHILD ANIMATION CODE // needed to execute before parent update cleanup code

}
}

I need access to the child inbetween the update of the parent.

Should I just copy paste the update function and fully override it and just cutout the child parts in the parent?

This gets messy if I need to alter the parent update code, I will also need to go rewrite the override I did on the child

The way this is typically solved in object oriented programming is to introduce a special empty method in the parent that can be overriden in the child.

public class ParentClass : MonoBehaviour
{
void Update(){
   //some movement code valid for all parents and child

   doAnimation();

   //some cleanup code valid for all parents and child
}

protected virtual void doAnimation() {}
}

Sometimes it makes even sense to decompose the whole method to enable child classes to override all of the different aspects if they need to.

public class ParentClass : MonoBehaviour
{
void Update(){
   doMovement();
   doAnimation();
   doCleanup();
}
4 Likes

Ah this makes a lot of sense, thank you very much!