Interview Question
Software Engineer / Developersyes you can call but with this approach you have to always figure out the type of the derived class's pointer but by accessing the derived class functions(which are virtual function) through base class pointers you don't have to worry about the pointer type and it will automatically invoke the desired type function. That's the advantage of virtual functions.
plz correct me if I am wrong.
This is concept is necessary for extensibility/flexibility as others mentioned. You have a decision whether to call base/derived now. But, say in future you want to add one more derived class with a different implementation and you want to call this new derived class's function, you can do that with virtual functions by deferring the call till runtime.
with out virtual function,
if you have 5 Derived class, then you have to make 5 conditions in the main process to call different function from the 5 Different class. moreover, if someone extend a few class from your Derived class or he just want to have a few more Derived class, then he have to update the main process.
VF concept is used to make decision at run time which function is to be called... if you know that you are going to make a function call to derived/base class before hand then it doesn't make sense to use VF...
- Somebody June 18, 2011