Read this article in your language IT | EN | DE | ES
Overriding
Suppose you have a base class ClassA in which you have written a method MethodA. Now, if you want to inherit this class in a ClassB and use MethodA then the MethodA declared in ClassA will be used, but if you override this method in your inherited class classB then it will be over-ridden and the implementation of classB will be used whenever we use it from classB, and the implementation of classA will be used whenever we use it from a instance of classA.
using System;
using System.Diagnostics;
using System.IO;
public class ClassA
{
public virtual void MethodA() {
Trace.WriteLine("BaseClass MethodA");
}
}
public class ClassB :ClassA{
public override void MethodA() {
Trace.WriteLine("SubClass MethodA overridden");
}
}
//implementation class for using the above defined classes
public class TopLevel {
static void Main(string[] args) {
TextWriter tw = Console.Out;
Trace.Listeners.Add(new TextWriterTraceListener(tw));
ClassA obj = new ClassB();
obj.MethodA(); // Output will be “Subclass MethodA overridden”
}
}
Shadowing instead of overriding
Up to now the code shown was for overriding a base class method with the child class method, but what will happen if you want to provide the base class behaviour instead, use the new directive, with or without virtual at the base class is this possible in C#? Yes, it is and that is the concept of shadowing in C# using a keyword "new' which is a modifier used instead of "override".
public class ClassA
{
public virtual void MethodA() {
Trace.WriteLine("ClassA Method");
}
}
public class ClassB : ClassA {
public new void MethodA() {
Trace.WriteLine("SubClass ClassB Method");
}
}
public class TopLevel
{
static void Main(string[] args) {
TextWriter tw = Console.Out;
Trace.Listeners.Add(new TextWriterTraceListener(tw));
ClassA obj = new ClassB();
obj.MethodA(); // Outputs “Class A Method"
ClassB obj1 = new ClassB();
obj1.MethodA(); // Outputs “SubClass ClassB Method”
}
}
Polymorphism & Virtual Methods
A virtual method in C# specifies an implementation of a method that can be polymorphicaly overridden derived method. A non-virtual method can't be polymorphically override in a Derived class.
When we declare a virtual method, it must contain a method body. Otherwise the compiler will generate an error. Remember that, since virtual methods are used for achieving polymorphism and since polymorphism works only with objects, it is not possible to declare a static method as virtual in C#. Similarly the private methods are also not possible to declare virtual, since they can't override inside a derived class.
In C#, it is not necessary to override a Base class virtual method inside a Derived class. The following program will work absolutely correct.
using System;
class Base
{
public virtual void Method()
{
Console.WriteLine("Base method");
}
}
class Derived : Base
{
}
class MyClient
{
public static void Main()
{
Derived d1 = new Derived();
d1.Method(); // Displays 'Base Method'
}
}
Or even it is possible to override a virtual method non-polymorphically inside a Derived class (method can be virtual method in derived class) as shown below.
using System;
class Base
{
public virtual void Method()
{
Console.WriteLine("Base method");
}
}
class Derived : Base
{
public virtual void Method()
{
Console.WriteLine("Derived method");
}
}
class MyClient
{
public static void Main()
{
Derived d1 = new Derived();
d1.Method(); // Displays 'Derived Method'
}
}
Even it is possible to omit the keyword virtual from the Derived class method
using System;
class Base
{
public virtual void Method()
{
Console.WriteLine("Base method");
}
}
class Derived : Base
{
public void Method()
{
Console.WriteLine("Derived method");
}
}
class MyClient
{
public static void Main()
{
Derived d1 = new Derived();
d1.Method(); // Displays 'Derived Method'
}
}
And also it is possible to declare the Derived class method as new.
using System;
class Base
{
public virtual void Method()
{
Console.WriteLine("Base method");
}
}
class Derived : Base
{
public new void Method()
{
Console.WriteLine("Derived method");
}
}
class MyClient
{
public static void Main()
{
Derived d1 = new Derived();
d1.Method(); // Displays 'Derived Method'
}
}
When we want to override a virtual method polymorphically inside a Derived class, we must have to use the keyword override along with the method declaration. Also the Base class method must declare using the keyword virtual. This is what is known as Polymorphism in C#. The example is shown below.
using System;
class Base
{
public virtual void Method()
{
Console.WriteLine("Base method");
}
}
class Derived : Base
{
public override void Method()
{
Console.WriteLine("Derived method");
}
}
class MyClient
{
public static void Main()
{
Base b1 = new Derived();
b1.Method(); // Displays 'Base Method'
}
}
Note: An overridden declaration must be identical in every way to the virtual method, it overrides. They must have the same access level; the same return type, the same name and same method signature. The overridden methods are implicitly virtual in nature and hence they can override in subsequent Derived classes. As like virtual methods, override methods can't be declared as static or private.