Jump to content
  • Advertisement
Sign in to follow this  
Uphoreum

[.net] [C#] Inheritance and Overriding

This topic is 4223 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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()?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Yep, that answers my question (it's not your fault, I was just having difficulty finding the words for my question)

Share this post


Link to post
Share on other sites
Here's a pretty exhaustive reference. It'll still probably be useful, despite the conclusion of this topic. [smile]

-jouley

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!