tags:

views:

84

answers:

2

Hi,

I have an abstract class with some methods,including an abstract method(Execute()).This method is overridden in child class.Now, an event is raised(somewhere in application),and for this event there is a handler in base class.And,in this handler,I call Execute. Now, the method of chilobject is executed.I am bit confused,how this works under the hood?

A: 

This is runtime polymorphism, also called dynamic binding.

Matthew Flaschen
A: 

One way to think of this is in terms of message passing; the call of Execute() inside an instance method means that the “message” Execute is sent to the current object (i.e. this). Since the current object is an instance of the child class, it treats the received message “Execute()” as is defined for all instances of that class, regardless of where the command to send that message is written.

(If your question was how it's technically implemented, then I'm sorry for the simplistic answer. In very general terms the class of the object is stored with the rest of its data and this is used to look up the appropriate method to call, i.e. how to react to a certain message to that object.)

Arkku
The essential difference between a method and a message, in my view, is that when you call a method on an object, you know it will be dispatched to some class's method. When you pass a message, you may end up with "Message not understood".
Matthew Flaschen