Question about Overriding

This may be a stupid question to ask but I don’t understand why this is happening as to me, it shouldn’t.

I have 3 scripts, A Objects script, A Floating Object script and a Asteroid script. The asteroid script Inherits the floating object script and the floating object script Inherits the objects script.

I have a protected void Update method in the Objects script and a protected virtual void Update in the floating object script. Basically what I don’t understand is how everything still works since the floating object’s Update method isn’t overriding the objects Update method but I still need to call the base Update method and still allows me to use my own methods within the floating object script.

I basically thought that if you wanted to add your own code into a parents Update method then you needed to override it and call the base method first but I’m not here and it still works correctly as if I was overriding the base Update method.

I hope any of that makes sense. Here are the methods I’m referring too, first method in the Objects script and the second method in the Floating Objects script:

protected void Update()
	{
		if(!GetComponent<SpriteRenderer>().isVisible)
		{
			destroyTimer += Time.deltaTime;
			if(destroyTimer >= destroyDelay)
			{
				Destroy(gameObject);
			}
		}
	}

protected virtual void Update()
	{
		base.Update();
		RotateObject();
	}

I was wondering, if you understand it, if you could explain to me why everything still works correctly despite me not overriding the base Update method and I’m sorry if this is a stupid question to ask.

So to be clear, you have something like this:

public class Object : MonoBehaviour{
    protected void Update() {
        Debug.Log("Inside Object"); //I'll use this for a demo later
    }
}

public class FloatingObject : MonoBeheaviour{
    protected virtual void Update{
        base.Update();
        Debug.Log("Inside Floating Object"); //I'll use this for a demo later
    }
}

When Update is called on FloatingObject, your output is:

Inside Object
Inside FloatingObject

And you are wondering why/how this is working.

Well first, you are correct in saying that you are not overriding the Update method inside Object:

GameObject obj = /*get a reference to an object with a FloatingObject component*/;

FloatingObject floating = obj.GetComponent<FloatingObject>();
floating.Update(); //logs "Inside Object, Inside Floating Object"

Object baseCast = (Object)floating;
baseCast.Update(); //This will log "Inside Object" because you are not overriding. If you were overriding, you'd have the same output as above

I am guessing your confusion is that you are able to call base.Update(). The fact that you can do that actually has nothing to do with overriding. When you prefix a method call with base. all that happens is the given method from the base class is called. You could call ANY method that exists in the base class inside of FloatingObject.Update. For example, lets say we added public void ObjectMethod(){} to the Object class. This would still be perfectly valid:

//Inside FloatingObject
protected virtual void Update() {
    base.ObjectMethod();
    //Other update stuff here
}

All base. does is restrict the call to methods existing in the base class. When you override methods, this allows you to call the base class version of Update. However, you do not NEED to call the base method. If you wanted to completely replace Object.Update with FloatingObject.Update you can ignore the base.Update call.