So apparently you cannot use the virtual
modifier with the override
modifier.
virtual
- a method that can be overridden
override
- a method that is overriding a method of the same name in its parent's class
This leads me to believe that if I override a method in a child class, if that child has a child you can't override that method again.
And it is safe to say that if you put override
and virtual
in a method declaration you will get a compile error in C#.
However I can't understand why the code I made below works the way in which it does
using System; public class DrawingObject { public virtual void Draw() { Console.WriteLine("Drawing Object"); } } public class DrawDemo { public static int Main() { DrawingObject[] dObj = new DrawingObject[3]; dObj[0] = new DrawingObject(); dObj[1] = new Line(); dObj[2] = new LittleLine(); foreach (DrawingObject drawObj in dObj) { drawObj.Draw(); } Console.Read(); return 0; } } public class Line : DrawingObject { public override void Draw() {// the method above me is in fact virtual because LittleLine overid it? Console.WriteLine("I'm a Line."); } } public class LittleLine : Line { public override void Draw() { Console.WriteLine("I'm a Little Line."); } }
Here's the output:
Drawing Object
I'm a Line.
I'm a Little Line.
So the draw method in Line
looks as though it was overridden by LittleLine
. Is this code not actually overriding it, or is the compiler doing some other trick? Or am I not understanding the context of virtual
and override
?
Because virtual functions are called only for objects of class types, you cannot declare global or static functions as virtual . The virtual keyword can be used when declaring overriding functions in a derived class, but it is unnecessary; overrides of virtual functions are always virtual.
Method Overriding in C# is similar to the virtual function in C++. Method Overriding is a technique that allows the invoking of functions from another class (base class) in the derived class. Creating a method in the derived class with the same signature as a method in the base class is called as method overriding.
Use virtual for the base class function declaration. This is technically necessary. Use override (only) for a derived class' override.
You can declare a certain method as virtual
only once, but you can override
it as many times as you want - an override is not final, and it does not limit classes that inherit from the first overriding class. The method that will eventually execute is the last one the overrides the virtual method. So your code does behave as expected.
C# is very verbose with regard to overriding - you have more specifiers than C++ or Java. It is so to let the programmer specify the exact intent:
virtual
to specify a method can be overridden by subclasses. override
to specify you are overriding a method that you know is virtual (if it's not, the compiler will report an error). sealed
to prevent further overriding. new
to hide instead of override. This can be confusing and sometimes annoying, but it ensures you actually know what you're doing, and it makes your intention self-documented.
So apparently you cannot use the virtual modifier with the override modifiers.
Indeed. The method stays virtual unless you override it with a method which is also declared to be sealed
. (You can only use the sealed
modify on a method when you're overriding something.) From section 10.6.5 of the C# 4 spec:
When an instance method declaration includes a sealed modifier, that method is said to be a sealed method. If an instance method declaration includes the sealed modifier, it must also include the override modifier. Use of the sealed modifier prevents a derived class from further overriding the method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With