Greetings,

I'm working on an existing solution. The solution is using a GUI inheritance technique. All Forms within the solution inherite from a parent Form. Within the parent Form there is a virtual method with empty body that is called in the parent Form load event. Each Form override that method and provide its specific implementation. However, when running the solution the derived class method version is the one that is executed. That is confusing me how come the derived class override version is executed while all what is done is overriding the parent version in the derived classes while not invoking this derived version explicitly in any single line of code!!!!

Recommended Answers

All 3 Replies

Since the forms derive from a form that calls the method in the load event each derived form will also call the method, but because that method is overriden the specific one to the derived form is used. If you want to override that behaviour you should be able to override the load event handler

What I know about that issue is that when you do inheritance and you override a method X in the parent class P within a child class C then you make a child class instance Z and you do

Z.X()

So you are invoking the child class version and parent class version is not invoked, however, if you want to invoke the parent class version using the child class instance you must write

base.X()

Within the override version of the method. So when you invoke the child class version it will invoke the parent class version. However, if we make a parent class instance Y and do

Y.X()

The parent class version of the method is invoked and the child class version is not invoked and there is no way to invoke the child class version within the parent class version as there is nothing such child.X() like base.X().

So what is confusing me is that the parent Form method has an empty body and it is invoked within the parent Form load event handler. I know that the child Form load event handler will invoke the parent Form load event handler implicitly and because the parent Form method is invoked in its load event handler it will be executed, however, it has an empty body.

What you are telling me now is that because this method is overriden in the child Form instead of invoking the parent Form version the child Form version is invoked, however, there is no explicit invocation to the child Form version in any single line of code in any where within the solution!!! if that is what you mean this will be a new information for me.

Inheritance can create some strange dependencies.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.