Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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...

    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 23-Mar-10 6:34am
Edited 23-Mar-10 6:44am
v4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Virtual methods are extremely useful but can also be abused.
 
Imagine a base class that has a method SetText
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.
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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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... Smile | :)
void direved::go()
{
  take_a_smile();
  base::go();
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
Smile | :)
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 476
1 Sergey Alexandrovich Kryukov 265
2 CHill60 195
3 Afzaal Ahmad Zeeshan 145
4 F-ES Sitecore 120
0 Sergey Alexandrovich Kryukov 9,121
1 OriginalGriff 7,920
2 Peter Leow 4,774
3 Kornfeld Eliyahu Peter 3,210
4 Maciej Los 2,176


Advertise | Privacy | Mobile
Web02 | 2.8.150327.1 | Last Updated 23 Mar 2010
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100