1

From my understanding, the virtual keyword allows you to use the base class' method, and override allows you to override it in a class that inherits from the base class. My confusion is that I just tested not using the virtual keyword in the base class' method definition and not including that method in the derived class, and I was still able to call the base class' method (it showed up in intellisense and ran). Also, I know that if i wanted a new method definition for that same method for my derived class that I could use the new keyword..

I am probably missing a key point but this is the way I understand it which is why I am confused as to the purpose of virtual and override

bruchowski
  • 5,043
  • 7
  • 30
  • 46
  • possible duplicate of [C# keyword usage virtual+override vs. new](http://stackoverflow.com/questions/159978/c-sharp-keyword-usage-virtualoverride-vs-new) – BrokenGlass Nov 20 '11 at 17:43

5 Answers5

3

The power of those keywords comes into play when you use polymorphism

I think an example helps understanding the most. Consider the following:

class Base {
  public virtual void f() { Console.WriteLine("Virtual Base"); }
}

class Override : Base {
  public override void f() { Console.WriteLine("Overridden derived"); }
}

class New : Base {
  public new void f() { Console.WriteLine("New derived"); }
}

Now, when you have an object of actual type Derived but static type Base, calling f() still will output "Overridden Derived":

Base obj = new Override();
obj.f(); // Overridden derived

With the new keyword you tell the runtime to stop looking for a method with that name starting from that class:

Base obj = new New();
obj.f(); // Virtual Base

In contrast to calling f() on a of type at least New:

New obj = new New();
obj.f(); // New derived
knittl
  • 246,190
  • 53
  • 318
  • 364
2

My confusion is that I just tested not using the virtual keyword in the base class' method definition and not including that method in the derived class, and I was still able to call the base class' method (it showed up in intellisense and ran).

This is expected behaviour.

Virtual allows you to override a method which is defined in the base class, in other words extend the implementation of the method in the derived class.

Differences between the new keyword and the override one can be found on MSDN here.

It is access modifiers (private, public, protected) that affects if you are able to call the base class method in the derived class or not.

Robin Andersson
  • 5,150
  • 3
  • 25
  • 44
0

Virtual allows you to override the base class method. You can still call the method without it but will not be able to override the method without it.

Sorry, I forgot to mention polymorphism. So if you have a base class that defines a method you can do something like this.

The below snippet will print from B

public class A
{
  virtual void foo
  {
    Console.WriteLine("From A");
  }
}

public class B : A
{
  override void foo()
  {
   Console.WriteLine("From B");
  }
}

static void Main(string[] args) 
{
   A testA = new B();

   a.foo();
}
  • Isn't that what the "new" keyword does? I just tested it and it ran the method from my derived class when I use the "new" keyword – bruchowski Nov 20 '11 at 17:23
  • @Maximus9000: the important concept here is [polymorphism](http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming) – knittl Nov 20 '11 at 17:46
0

To override a method in a base class the method you are overriding must be marked as virtual - you override a method using the override key word.

new in the context of a method is slightly different. It allows you to add a method in a subclass with the same name as a method in the base class. Which method gets called will depend on the reference that you use to access the method.

You can access any methods implemented in the base class (subject to access modifiers) regardless of whether they are marked virtual or not.

s1mm0t
  • 6,035
  • 4
  • 38
  • 45
  • So in essence, you can use the 'new' keyword to effectively override a base class' method, it is just more proper to do it with virtual and override? – bruchowski Nov 20 '11 at 17:29
  • When using new you are not overriding, you are just adding another method which happens to have the same name as a method in the base class. – s1mm0t Nov 20 '11 at 17:31
0

When you mark a method as virtual or abstract, run-time type checking is used to determine the actual implementation of the method to call: it checks for the "most derived type" that implements/overrides the virtual method, and invokes that implementation. A plain method does not require run-time type checking.

Thus, the distinction between virtual and non-virtual has a meaningful impact on the semantics of your code, especially for public APIs. If you see this:

class A { void foo(); virtual void bar(); }
...
A myObject = getSomeObject();
myObject.foo();
myObject.bar();

You can be sure that the implementation of foo that is invoked is the one declared in class A. This is because only compile-time type checking is used to determine the method to be called. However, the specific implementation of bar that is invoked may be the implementation in some subclass of A, depending on the run-time type of myObject.

More info: http://msdn.microsoft.com/en-us/library/aa645767(v=vs.71).aspx

Mike Mertsock
  • 11,825
  • 7
  • 42
  • 75