How does abstract function differs from virtual function?
I am beginner programmer in the programming world. I am learning the programming C++, C sharp, Java, .NET and DBMS. Now a days, i am working on the Java and C sharp language. I want to know how does abstract function differs from virtual function, in which case it is better to use abstract or virtual function? Please help me.
Re: How does abstract function differs from virtual function?
According to me, the virtual function doesn't work on classes. They are only meant for a class method, properties, indexer or event declaration to have the possibility, and to override them in the derived class. And on the other side, the abstract class cannot be initiated and can more interpreted as a template of a class, which has to be derived to make an instance of it. Important to note that any class with abstract method or property in it should be declared as abstract. Hope your problem will be fix.
Re: How does abstract function differs from virtual function?
I think that virtual method/function contains only the implementation and it provide the derived class with the option/way of overriding it. Whereas the abstract method doesn't provide/contain an type implementation and it forces the derived class to override the method. An abstract class may contain abstract methods and accessors. Also note that trying to instantiate an object of an abstract class results in a compile error. Hope you will understand the term virtual and abstract.
Re: How does abstract function differs from virtual function?
Abstract method/function: A class which will have the abstract methods/functions, then that class should be declared as abstract. The abstract method/function does not have implementation and thus, classes that derive from that abstract class, must provide an implementation for this abstract method.
Virtual method: Any class can contain the virtual method/function. The virtual does not work on classes. This virtual method/function comes with an implementation. And as you inherit from a class which has a virtual method, you can override the virtual method and provide additional logic, or replace the logic with your own implementation.
Re: How does abstract function differs from virtual function?
I think this two keywords(i.e virtual and abstract) are many confusing because both of them can be overridden. But abstract members does not have implementation, whereas virtual members have. The Abstract keyword can be given to either members or the class as a whole. Also note that, if a class contains the abstract member, then that class should be itself the abstract, in this not required in virtual case. This are the only difference which i know, hope your problem is fixed.
Re: How does abstract function differs from virtual function?
My knowledge about the programming world says that abstract methods/functions are always consider as virtual. And they do not have any implementation. This is only the main difference between them. Usually, we will use the virtual method/function if we have a ''default'' implementation of it and want to allow descendants to change its behavior. And with abstract method we will force descendants to give the implementation. Hope your problem will be solved.