• Advertisement
Sign in to follow this  

[.net] [C#] Inheritance and Overriding

This topic is 4038 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
Yeah the stuff can be a pain until you get the terms down. But hey we have all been there.

theTroll

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
Really? I figured since it was no longer marked as "virtual", it couldn't be overriden, but I guess it was simpler than I thought. Thanks!

Share this post


Link to post
Share on other sites
You just override. In Player, "base" refers to Rotatable. In Rotatable, "base" refers to Entity. So if in Player, you call base.Update, that will go into Rotatable::Update. And "base.Update" in Rotatable::Update will go into Entity::Update and tada - all updates occur. There's no problem.

Then if you have an Entity reference that happens to be to a Player and you call ent.Update() it'll go into Player::Update.

This is what "virtual" means - that the compiler will generate the code to figure out what function to execute based on the runtime state of the reference variables. If Update was non virtual and you did this:


Entity ent = new Player();
ent.Update();


The ent.Update() call would go into Entity::Update. But if you downcast the Entity reference to Player thusly:


Entity ent = new Player();
Player p = (Player)ent;
p.Update();


Then execution would go into Player::Update because that's the formal type on the variable. Fortunately, Update is virtual and so you don't have to worry about it.

Share this post


Link to post
Share on other sites
Woa, I just noticed another thing. It seems that my base class's private variables are not accessible from the derived class. I figured that if I had this:

public class MyClass
{
private int variable;
}

I could access it like this:

public class MyDerivedClass : MyClass
{
public void MyMethod()
{
variable = 0;
}
}

But private members don't show up in the IntelliSense list. Is this supposed to happen?

Share this post


Link to post
Share on other sites
Quote:
Original post by Uphoreum
Is this supposed to happen?


Absolutely. Note the distinction between the private modifier and the protected.

Share this post


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

  • Advertisement