Reputation: 11085
I have knowledge of Java and have been learning C# for the last couple of days. Now I have come across the "virtual" keyword which, as suggested at this link, is used to allow the corresponding methods, properties etc. to be overriden in the subclasses. Now I think we can override methods even without using the "virtual" keyword. Then why it is necessary?
Upvotes: 15
Views: 8424
Reputation: 27036
Take a look at this example:
void Main()
{
var obj1 = new Sub();
Console.WriteLine(((Base)obj1).method1());
}
public class Base
{
public virtual string method1()
{
return "method1 (from Base)";
}
}
public class Sub : Base
{
override public string method1()
{
return "overridden method1 (from Sub)";
}
}
In this example the subclass Sub
is instantiated and assigned to object variable obj1
.
In the Console.WriteLine
statement's parameter it is being cast into the base class Base
, then method method1
is called.
Because we have used virtual
in the base class Base
and override
in the subclass Sub
, we're getting
overridden method1 (from Sub)
as output.
If you remove virtual
in the method declaration of method1
in Base
and remove override
in the method of subclass Sub
as well, then you're getting
method1 (from Base)
as output, which means that the cast also has the effect that the original declaration of method method1
in the base class Base
is used.
N.B. If you're using the new
keyword in subclass Sub
, such as
new public string method1() {
return "overridden method1";
}
assuming that the virtual
keyword in the base class Base
is absent, you're getting the output
method1 (from Base)
as well. If you would not cast it to Base
(and keep the type as Sub
), the new method1
would be used, meaning it belongs to the class Sub
where it is declared only - and hides the method's original declaration from the Base
class at the same time. This is called shadowing (or hiding) a method.
Summary:
To override a method, which should be effective also if you cast to the base class, use the virtual
keyword in the base class and override
in the sub class.
If you intend to override the method, which should be active in the sub class only, use the new
keyword in the sub classes method declaration. As you have seen it works also without it, but it is better if it is there so everyone knows that this is a new version of the method.
Upvotes: 2
Reputation: 25234
virtual
is a way of defining that a method has a default implementation, but that that implementation may be overriden in a child class. Other than by using virtual, you cannot override a method directly without using the new
keyword (which is generally bad practice).
A good example of the implementation of virtual
is the ToString()
method. Every object in C# is guaranteed to be able to call ToString()
because every object inherits from the base System.Object
class, which contains a virtual method ToString()
. Derived classes can override this however, and provide their own implementation which may be more useful to the users of the object.
Update: I recently wrote a blog post which goes into this topic in a bit of depth. Check it out here.
Upvotes: 9
Reputation: 52534
virtual / override will allow the derived class to override the base function if the derived class is instantiated as the Base class. Example:
class A
{
public virtual void Show() { Console.WriteLine("A"); }
}
class B : A
{
public override void Show() { Console.WriteLine("B"); }
}
A a = new A();
B b = new B();
a.Show();
b.Show();
A a1 = b;
a1.Show(); // here is the test!!!
B b1 = new B();
b.Show();
With virtual / override :
A
B
B
B
After removing virtual / override keywords (or simply removing override):
A
B
A
B
Upvotes: 0
Reputation: 56727
You need the virtual
keyword if you really want to override
methods in sub classes. Otherwise the base implementation will be hidden by the new implementation, just as if you had declared it with the new
keyword.
Hiding the methods by "overriding" them without the base method being declared virtual
leaves you without polymorphism, that means: if you "cast" a specialized version to the "base" version and call a method, always the base classes implementation will be used instead of the overridden version - which is not what you'd expect.
Example:
class A
{
public void Show() { Console.WriteLine("A"); }
}
class B : A
{
public void Show() { Console.WriteLine("B"); }
}
A a = new A();
B b = new B();
a.Show(); // "A"
b.Show(); // "B"
A a1 = b;
a1.Show(); // "A"!!!
Upvotes: 21
Reputation: 1039498
Now I think we can override methods even without using the "virtual" keyword.
No, you can't. Contrary to Java, in C# members are sealed by default and you cannot override them unless you marked them with the virtual
keyword.
Upvotes: 2