Sign in to follow this  

[.net] Inheritance problems

This topic is 4837 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 override a function used in a base class constructor in the inherited classes. This generally works out fine, but occasionally the function overridden in the inherited class needs a property from the inherited class. The problem is that this property has not yet been initialized when the base class constructor runs. I take it there is no way to delay the base class constructor? here's a pseudocode example class Dog { public Dog() { this.InitializeStuff() } protected virtual void InitializeStuff(){do stuff} } class Boxer : Dog { private object something; public Boxer(object Tthing): base() { this.something = Tthing; } protected override void InitializeStuff() { use this.something } } I could move the use of InitializeStuff() from the base constructor to all the inherited constructors, but this could require another developer to do the same thing. I'd rather someone be able to just override the function and be done with it. Is there a better solution to the problem, or is the mere fact that I face this issue a sure sign that I am a bad developer?

Share this post


Link to post
Share on other sites
What you should do is initialize the stuff from the base class in the base class and the stuff from the derived classes in the derived classes. If you really need to initialize base class members in the derived class you could provide protected get and setters or make a specialized constructor in the base class and call it in the constructor in the derived classes

Cheers

Share this post


Link to post
Share on other sites

This topic is 4837 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this