[.net] Inheritance problems

Started by
0 comments, last by ernow 19 years, 7 months ago
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?
Behold the nascent power of Semejant!
Advertisement
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

This topic is closed to new replies.

Advertisement