GeekInterview.com
Series: Subject: Topic:
Question: 397 of 460

What does the keyword virtual mean in the method definition?

The method can be over-ridden.
Asked by: Interview Candidate | Asked on: Sep 15th, 2004
Showing Answers 1 - 4 of 4 Answers
sahu

Answered On : Jan 9th, 2006

View all questions by sahu   View all answers by sahu

Ans:

over-ridden.

  
Login to rate this answer.
learnasp

Answered On : Oct 11th, 2007

View all answers by learnasp

It means that the subclasses( inheriting classes) can override the method.

Yes  1 User has rated as useful.
  
Login to rate this answer.
sahajanandk

Answered On : Mar 9th, 2009

View all answers by sahajanandk

Keyword virtual is used to make changes to the methods inside the virtual class while runtime. the virtual class should be overide with the same modifier, return type and the name.

  
Login to rate this answer.
smandke

Answered On : Aug 27th, 2010

View all answers by smandke

Virtual method in C# is a used to support  polymorphism. This means that in a base class we will define a method foo1() with the keyword "virtual" and the derived classes will inherit this behavior of this method. 


say  
class Base
{
      public virtual int foo1()
      {
          return 0;
      }

      public virtual int foo2()
      {
          return 0;
      }

}


Next - if the derived class wants to override this behavior then the override keywork is use
class Derived : Base
{
       public override int foo2()
       {
              return 1; 
        }
}


public class Tester
{
static void main()
{
      Base b = new Base();
     Derived d = new Derived();

    int  first =  b.foo1();
    int second = d.foo1();

// compare values of the 2 variables first and second. - ensure variable values is 0 -  next

    int  third =  b.foo2();
    int forth = d.foo2();  

// compare values of the 2 variables third and forth. - ensure variable values is 0 and 1 

}

}
Hope this clarifies the usage of the keywork virtual and override

Thanks,
Shaila

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.