Difference between Abstract and Virtual functions
Mahesh Kumar alanka
Abstract function:--> 1) An abstract function has no implementation. It can only be declared. This forces the derived class to provide the implementation of it. 2) Abstract means we MUST override it. 3) An abstract member is implicitly virtual. Abstract can be called as pure virtual in some of the languages.Virtual Function:--> 1) Virtual Function has an implementation. When we inherit the class we can override the virtual function and provide our own logic. 2) We can change the return type of Virtual function while implementing the function in the child class(which can be said as concept of Shadowing). 3) Virtual means we CAN override it.
1. An abstract function cannot have functionality. You're basically saying, any child class MUST give their own version of this method, however it's too general to even try to implement in the parent class.2 A virtual function, is basically saying look, here's the functionality that may or may not be good enough for the child class. So if it is good enough, use this method, if not, then override me, and provide your own functionality.
1. abstract function doesn't contain any body but a virtual function contain body 2. we must be implement the abstract function in derived class but it is not necessary for virtual function 3. abstract function can only use in abstract class but it is not necessary for virtual function 4. abstract function are called pure virtual function
Abstract Functions can not have any body or implementation while virtual Functions can have body or implementation ,abstract functions must be implementation by derived classes and virtual not must implementation by derived classes,both are override in derived classes
/*************************************************************** * Abstract method has no body but Virtual method has body. * Abstract method declared in the Abstract class but Virtual method need not Abstract class * Abstract method must be override in the derived class but Virtual method may be override or not * Absract method is called pure virtual method * We can not create object of Abstract class but we use like -- AbstractClass obj = new ChildClass() *****************************************************************/ using System; namespace LearnProject { abstract class TestClassA { public void Method1() { Console.WriteLine("I am in Method1()of TestClassA "); } public virtual void Method2() { Console.WriteLine("I am in Method2() of TestClassA "); } public abstract void Method3(); } class TestClassB : TestClassA { public override void Method2() { Console.WriteLine("I am in Method2() of TestClassB"); } public override void Method3() { Console.WriteLine("I am in MethodB() of TestClassB"); } } class Abstract_and_Virtual_methods { public static void Main() { Console.WriteLine("================================"); TestClassB testClassB = new TestClassB(); testClassB.Method1(); testClassB.Method2(); testClassB.Method3(); Console.WriteLine("================================"); TestClassA testClassA = new TestClassB(); testClassB.Method1(); testClassA.Method2(); testClassA.Method3(); // TestClassB b = new TestClassA() is not allowed TestClassB b = new TestClassB(); b = testClassB; // Object of TestClassB is assigned b = testClassA as TestClassB; // Here We need type cast Console.Read(); } } }
http://softwarecafeblog.blogspot.in/2011/05/abstract-vs-virtual-in-c.html