What is virtual keyword and how to use it?


In this article I'm going to explain What is virtual keyword and how to use it? Virtual function are normal member functions of class which can be over ridden in the derived classes. The whole functionality can be replaced in the over riding function. Virtual function can be declared with a keyword "Virtual".

In this article I'm going to explain What is virtual keyword and how to use it? Virtual function are normal member functions of class which can be over ridden in the derived classes. The whole functionality can be replaced in the over riding function. Virtual function can be declared with a keyword "Virtual". By default , methods are non virtual. You can override a non virtual method. You can not use the virtual modifier with the abstract,private,static or override modifiers.


Base class


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

///
/// Summary description for Class1
///

public class Class1
{
public Class1()
{
//
// TODO: Add constructor logic here
//
}
virtual public string FunctionA()
{

return "FunctionA";


}

virtual public string FunctionB()
{

return "FunctionB";

}

}



DerivedClass :-



using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


public class Class2: Class1
{
public Class2()
{
Class2 b = new Class2();

b.FunctionA();
b.FunctionB();


}
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: