[.net] [C#] Inheritance and Overriding

Started by
13 comments, last by Uphoreum 17 years, 2 months ago
I have an 'Entity' class, and a 'RotatableEntity' class that inherits 'Entity'. 'Entity' has an update method, and I need 'RotatingEntity' to add some code to it. I'm guessing that the way to do this would be to override update and call the base.Update(). So, as far as I know, to override in C#, the method to be overriden must be 'virtual'. If I make that method 'virtual' can I still call it when I make an instance of 'Entity', even though 'Entity's update method would be virtual? Like, I want to be able to do this: Entity entity; entity.Update(); and: RotatableEntity rotatable; rotatable.Update(); Will entity.Update() being virtual still allow me to: entity.Update()?
Advertisement
To call the base method in the inherited class you just need to do this.

base.MethodName();

So in your case; base.Update();

That is it.

theTroll
I know, but I'm asking what I should have the methods be (like, 'abstract', 'virtual', etc) to allow me to call both entity.Update() and rotatableEntity.Update() outside of the classes. Like, if entity.Update() is virtual, can I still call it like:

entity.Update();

outside of the Entity class? Like, in my main game class?
Yes, that is pretty much the point of OO. You can call entity.Update() and not worry what subclass entity actually is - it will call the Update() of the appropriate subclass.
It all depends.

If you create a enity object you can call the enity.Update no problem. If you create a RotatableEnitity object then you can call RotableEnitiy.Update() just fine. What you can't do is create a RotatableEnitity object and try to directly call the enity.Update() method.

So to handle this we make the Enity.Update() method as virtual. These means that we are expecting it to be overridden in all inherited classes.

No in Enity.Update we have all kinds of code that are basic to all Enity objects. So in the inherited RotatabaleEntity class we Create the Update() method. In the method we do the following..

Update()
{
base.Update();
// RotatableEntity code here...
}
This will call the Entity.Update() method first and then do the rest of the stuff you need done.

You can call the Entity.Update() method on any Entity object, and the RotatableEntitiy.Update() method on a RotatableEntity object. The only way to call the Entity.Update() method from the RotatableEntity object is to have the RotatableEntity class have a method that class base.Update();

I hope that explains what you are asking..

theTroll
Yep, that answers my question (it's not your fault, I was just having difficulty finding the words for my question)
Yeah the stuff can be a pain until you get the terms down. But hey we have all been there.

theTroll
Here's a pretty exhaustive reference. It'll still probably be useful, despite the conclusion of this topic. [smile]

-jouley
Uh oh, run into a bit of a problem. What if I want to override the overriden function? Basically, the heirarchy goes like this:

Entity->RotatableEntity->Player (for now).

Each of those has their own variation of the "Update" method. Entity simply moves, RotatableEntity adds rotation code, and Player adds acceleration code.

I tried overriding Entity's Update method in RotatableEntity, but it seems to want me to do it like this:

public override void Update()...

This means that I can't override it again in the player class right?

How do I deal with this? I need to override the same method twice. Is this not possible? Do I need to form my heirarchy in another way?
Ok, you are on the right track.

Once you make the base class as virutal you can override anything after it. So you can do what you want.

public virtual void Update() //Entity Update;
public override void Update() //Rotatable Update
public override void Update() //Person Update


Now if you write your person update like this;
public override void Update() //Person Update{     base.Update() // This calls Rotatable Update not Entity Update     // your person update code.}


Person.Update() can not directly call Entity.Update(), that can only be called through RotatableEntity.Update().

Does that make sense?

theTroll

This topic is closed to new replies.

Advertisement