override- virtual- new keywords

Hi everyone,

I have some doubts about override- virtual- new keywords.

I think that all methods who include override keywords are also virual methods since we are also able to override these kinds of overriden methods.

THe following code snippet demonstrate my opinion.

So I would like to ask that whether I am wrong or correct. If I am wrong, please make me correct.

class VirtualClass
{
  public virtual void VirtualMethod()
  {
    Console.WriteLine("VirtualClass:VirtualMethod");
  }
}

class OverrideClassA : VirtualClass
{
  public override void VirtualMethod()
  {
    Console.WriteLine("OverrideClassA:VirtualMethod");
    base.VirtualMethod();
  }
}

class OverrideClassB : OverrideClassA
{
  public override void VirtualMethod()
  {
    Console.WriteLine("OverrideClassB:VirtualMethod");
    base.VirtualMethod();
  }
}
 
Thanks,
Mert



Answer this question

override- virtual- new keywords

  • JapanFred

    When you overriden VirtualMethod() from base VirtualClass class, then the class OverrideClassA is introducing a new implementation of the inherited virtual VirtualMethod(), which means, it is no longer the same method as the VirtualMethod() declared in your base class.

    You can call the base implementation of VirtualMethod() using the base pointer like this:



    base.VirtualMethod();

     


    Regards,

    -chris

  • Alex Barry

    Thanks for the quick reply and I also wonder about something.

    class OverrideClassA : VirtualClass
    {
    public override void VirtualMethod()
    {
    Console.WriteLine("OverrideClassA:VirtualMethod");
    base.VirtualMethod();
    }
    }

    At the above class which is cut from the first reply's code snippet,

    is VirtualMethod() method the one which is virtual member of that class

    Thanks



  • dwielgosz

    Thanks for this clarification...

  • SuriyaPrakash

    javaboy,

    override modifier overrides the implementation of a class's virtual member, and at the same time, it inherits all the characteristics of the virtual member. At least that's the specification says.

    Regards,

    -chris

  • override- virtual- new keywords