Reputation: 5351
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
Upvotes: 1
Views: 3620
Reputation: 99
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();
}
Upvotes: 0
Reputation: 265918
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
Upvotes: 3
Reputation: 12025
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
Upvotes: 0
Reputation: 5390
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.
Upvotes: 2
Reputation: 6095
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.
Upvotes: 0