Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Suppose we have this fragment of code:
C#
abstract class Expression
{
	public abstract void Evaluate();
}

class Operation : Expression
{
	public override void Evaluate()
	{
		//stuff
	}
}


Now, when we use the written classes somewhere like below:
C#
Expression e = new Operation();
	
e.Evaluate();


How exactly does the runtime understand despite we have two Evaluate function and have referred our object e to the base class Expression, it uses the implemented function in the child class? If it's not a problem, please provide a gentle guidance unlike explanations in reference websites or books.

Thanks :)
Posted

You did not get any satisfactory answers so far. This is done via dynamic dispatch via the virtual method table. In other word, the required flexibility is achieved throw indirect call: the address is found in the table, and, for different runtime types, the address is different. The mechanism is explained here:
http://en.wikipedia.org/wiki/Dynamic_dispatch[^],
http://en.wikipedia.org/wiki/Virtual_method_table[^].

If you are not getting it, ask some follow-up question, I can explain fine detail.

For some advanced and indirectly related idea, please see my article: Dynamic Method Dispatcher[^].

It does not explain OOP mechanism, but it discusses the problems of dynamic dispatch and parallels with this mechanism, in the section "Complement OOP or Abuse it?".

—SA
 
Share this answer
 
Comments
Raul Iloc 20-Mar-14 13:39pm    
Why do you consider that your answer is the best?
In my opinion your answer is too complex for somebody that do not have basic OOP knowledge.
Sergey Alexandrovich Kryukov 20-Mar-14 13:44pm    
Okay, I got it. You down-voted your answer out of "revenge". This is dishonest.
--SA
Raul Iloc 20-Mar-14 13:51pm    
It seems that you have "big problems", and consider that only you have the right answers, even is not the case!
Sergey Alexandrovich Kryukov 20-Mar-14 14:11pm    
No, not only me, but certainly not you. I am talking only about this case. I talked to you as to a honest person, but you try to escape the matter of discussion and shift to personal tone. This is not acceptable here...
--SA
Raul Iloc 21-Mar-14 2:35am    
It seems that you have a strong programming background and you are coming from C++ world (like me), but you have very big problems regarding the communication, and this is pretty bad!
The abstract version is just a definition, so there is no implementation that can be executed. There are specific rules in the language that determine how it is done, see http://msdn.microsoft.com/en-us/library/sf985hc5.aspx[^].
 
Share this answer
 
This is one of the basic feature of OOP called polymorphism, and it works even your method from base class is not abstract (but must be virtual). See the next links for details:

MSDN-Polymorphism^]

Examples^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-Mar-14 11:27am    
Pretty bad. This is not yet polymorphism. You can talk about polymorphism only if you have a set of object. Late binding, yes, but, in this case, not solving any problems it was designed for.
—SA
Raul Iloc 20-Mar-14 14:01pm    
I do not agree with you! The question was put in a context of a set of objects.
Sergey Alexandrovich Kryukov 20-Mar-14 14:10pm    
This is not a matter of agreement, this is a matter of established culture, including terminology.
--SA
Raul Iloc 21-Mar-14 2:23am    
You are wrong again (2nd time), see the next link: http://en.wikipedia.org/wiki/Polymorphism_(computer_science)
PS: How you dare to make criticism without to give any arguments?
what has been done in the above code is that running time binding has been used,in the above even though 'e' is declared as an object of class Expression,what C# does it at runtime it allocates memory to 'e' on the basis of Operation class, as you might know that abstract classes cannot be instantiated.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-Mar-14 11:36am    
All wrong. Who asked you about allocation? Why the call is dispatched to this and not to another method? What if you have two non-abstract ones? Apparently, you have no clue yourself. Why answering?
—SA
SwarupDChavan 21-Mar-14 2:29am    
can you please specify how in the hell you are gonna do it with two abstract method,as far as I know you can inherit only a single class,just t0o show you are something great commenting on others,it shows even you dont have any idea.
Sergey Alexandrovich Kryukov 21-Mar-14 12:14pm    
Please, don't lie. Remember that everyone can read all the posts and see.
(Your comment is not comprehensible; what two abstract methods..? and so on...)

It's amazing: if out of frustration about some minor moment of criticism you are ready to lie and write gibberish, what are you going to do in some more serious conflicting situations? Please, control yourself. Again, remember that everyone can read your posts.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900