What is Virtual Function :
A virtual function or virtual method is a function or method whose behaviour can be overridden within an inheriting class by a function with the same signature.
Compile-time type and runtime type. With virtual methods, the runtime type is always used to determine the best method implementation. In the main entry point, ref1 has a compile-time and runtime type of A. On the other hand, ref2 has a compile-time type of A but a runtime type of B! With virtual methods, the runtime type is used in both invocations.
Example :
using System;
class A
{
public virtual void Test()
{
Console.WriteLine("A.Test");
}
}
class B : A
{
public override void Test()
{
Console.WriteLine("B.Test");
}
}
class Program
{
static void Main ()
{
// Compile-time type is A.
// Runtime type is A as well.
A ref1 = new A();
ref1.Test();
// Compile-time type is A.
// Runtime type is B.
A ref2 = new B();
ref2.Test();
}
}
Output
A.Test
B.Test
Restriction :
- We cannot use override keyword when it is not marked as virtual or abstract in base class.
Advantage :
- our project may be designed in such a way that you do not know all the types of objects that will occur when it is executed. You can provide a standard (base) type and design around that type. Then, you can re-implement important functionality depending on the more specific (derived) types. So when you call a method on the base type, you will invoke the more derived and useful method
- Whenever, there is a virtual function in the class, a v-table is constructed in the memory. The v-table has a list of addresses to the virtual functions of the class and pointers to the functions from each of the objects of the derived class. When a virtual function call is made, the v-table is used to get the addresses of the function and the function is called.
Practical Review
namespace TestConcepts
{
public partial class C_Examples_Part1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
VirtualBase obj = new VirtualBase();
string bas = obj.Virtual();
VirtualDerived objDer = new VirtualDerived();
string der = objDer.Virtual();
//Response.Write("Simple Virtual: " + bas + " - " + der);
VirtualSecond objSec = new VirtualSecond();
string sec = objSec.Virtual();
VirtualSecondDerived objSecondDer = new VirtualSecondDerived();
string objsecvir = objSecondDer.Virtual();
//Response.Write("Simple Virtual: " + sec + " - " + objsecvir);
//alternations
VirtualBase objFirst = objSecondDer;
string basFistAlternate = objFirst.Virtual();
Response.Write("<br>Base First + Second Derived: " + basFistAlternate);
VirtualBase VirtualBaseSecondBase = objSec;
string sVirtualBaseSecondBase = VirtualBaseSecondBase.Virtual();
Response.Write("<br>Base First + Secong Base: " + sVirtualBaseSecondBase);
VirtualBase VirtualBaseFirstDer = objDer;
string sVirtualBaseFirstDer = VirtualBaseFirstDer.Virtual();
Response.Write("<br>Base First + First Derived: " + sVirtualBaseFirstDer);
//VirtualDerived VirtualDerivedBaseFirst = obj;// Not able to assign - cannot allow implicit conversion
VirtualDerived VirtualDerivedBaseSecond = objSec;
string sVirtualDerivedBaseSecond = VirtualDerivedBaseSecond.Virtual();
Response.Write("<br>First Derived + Second Base: " + sVirtualDerivedBaseSecond);
VirtualDerived VirtualDerivedBaseSecondDer = objSecondDer;
string sVirtualDerivedBaseSecondDer = VirtualDerivedBaseSecondDer.Virtual();
Response.Write("<br>First Derived + Second Derived: " + sVirtualDerivedBaseSecondDer);
// VirtualDerived VirtualDerivedBaseSecondDer = objSecondDer;
// string sVirtualDerivedBaseSecondDer = VirtualDerivedBaseSecondDer.Virtual();
// Response.Write("<br>First Derived + Second Derived: " + sVirtualDerivedBaseSecondDer);
}
}
public class VirtualBase
{
public virtual string Virtual()
{
return "Virtual Base";
}
}
/*Base Class must contain Virtual Keyword when use same function in derived.
* otherwise implicitly it will take as 'new' */
public class VirtualDerived : VirtualBase
{
public override string Virtual()
{
return "Virtual Derived";
}
public string GetBaseVirutual
{
get { return base.Virtual(); }
}
}
public class VirtualSecond : VirtualDerived
{
new public virtual string Virtual()
{
return "Virtul Second";
}
}
public class VirtualSecondDerived : VirtualSecond
{
public override string Virtual()
{
return "Virtul Second Derived";
}
}
}
06:04 |
Category:
C# Concepts
|
0
comments
Comments (0)