Click here to Skip to main content
15,889,834 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone!
I'm currently a novice in C# programming....
I have a question: why do we need the virtual methods???
Please correct me if I'm wrong:
Virtual methods are the methods to be overriden in the derived classes.. Due to virtual methods base classes (base part of a derived class) can have access to the derived class overriden methods and fields.. Like in this example...

C#
 public class Base
 {
     virtual public void Go()
        {
           Console.WriteLine("Base class");
        }
  }

    public class Derived : Base
     {
           public override void Go()
           {
              Console.WriteLine("Derived Class");
           }
     }

/////...............
static void Main(string[] args)
{
     Derived der = new Derived();

// --- applying to the "base" part of derived class
((Base)der).Go();   // --- output is "Derived class"

}


So when I call the base virtual method it looks for the most derived override methiod in the derived classes and then returns the result to the base class method, thus changing the full implementation of it........
Does a compiler use any table (virtual tables of invocations)??
How to understand all that???

And one more example ....
when I create a button control on a form so I create
a Button class object ... this object has inherited overriden properties like Text, Name and etc...
So when I assign... button1.Text = "Run button";
so the override property sets its Text as "Run button" and then it
sends this overriden property "up in the class hierarchy" to the base classes ...to meet the ( virtual public string Text ) property
.....so what???? it changed the base class memeber, but I use the derived class member.....
Is it more effective to hide this property ....(by creating a field in derived class) ...I think it would be much effective, cause it can't be overriden and doesn't send any new implementation to the base classes??
Please explain.....
Posted
Updated 23-Mar-10 5:44am
v4

Virtual methods are extremely useful but can also be abused.

Imagine a base class that has a method SetText
C#
public virtual void SetText(string text)
{
    this.text = text;
}

public string Text
{
    get { return text; }
}
This method may be enough for the base classes purposes, but a derived class may prefer to validate the parameter or raise an event etc.
C#
public event EventHandler TextChanged;

protected virtual void OnTextChanged(EventArgs e)
{
    EventHandler eh = TextChanged;
    if(eh != null)
        eh(this, e);
}
public override void SetText(string text)
{
    text = ValidateText(text);
    if(Text != text)
    {
        base.SetText(text); // use the base's method!
        OnTextChanged(EventArgs.Empty);
    }
}
private string ValidateText(string text)
{
    string result = null;
    // validation procedure here
    return result;
}
You can see how the method has been made a lot more powerful but without changing the overall function of the method.

Note, I have deliberately called base.SetText. You should (normally) always call the base classes method when overriding to make sure any other functionality that may be part of that method is retained. Not doing so can lead to unpredictable results and possibly breaking of an application if the base class is later updated.
 
Share this answer
 
Nick Reshetinsky wrote:
Due to virtual methods base classes (base part of a derived class) can have access to the derived class overriden methods and fields..


It would be true for a reversed relation... :)
void direved::go()
{
  take_a_smile();
  base::go();
}
 
Share this answer
 
Actually, virtual methods *can* be overriden, but don't need to be unless the class (or method) is abstract.

Technically, you don't want to override properties because the set/get values. You should restrict your overriding to methods.
 
Share this answer
 
Because we want polymorphism, that is one of the pillars of OOP.
If you have any doubt about then you're strongly encouraged to read a good OOP book.
:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900