More with override in C#
Not Using "virtual" keyword
If you don't use the virtual keyword in the base class, you won't be able to use the override keyword in the derived class to override that method, property, indexer, or event. In this case, if you want to redefine the behavior of a method in the derived class without using virtual or override, you can use the new keyword.
Here's an example illustrating the use of the new keyword instead of override when the method in the base class is not declared as virtual:
Method without using
virtualandoverride:
using System;
public class Animal
{
// Method without the virtual keyword
public void Speak()
{
Console.WriteLine("Animal speaks");
}
}
public class Dog : Animal
{
// Use the new keyword to redefine the Speak method
public new void Speak()
{
Console.WriteLine("Dog barks");
}
}
public class Program
{
public static void Main()
{
Animal myAnimal = new Animal();
myAnimal.Speak(); // Output: Animal speaks
Dog myDog = new Dog();
myDog.Speak(); // Output: Dog barks
Animal myAnimalDog = new Dog();
myAnimalDog.Speak(); // Output: Animal speaks (base class method is called)
}
}In the above example:
The
Speakmethod in theAnimalclass is not declared with thevirtualkeyword.In the
Dogclass, thenewkeyword is used to redefine theSpeakmethod.When calling the
Speakmethod on a variable of typeAnimalbut referencing aDogobject, theSpeakmethod of the base classAnimalis called, not that of the derived classDog.
Property without using
virtualandoverride:
using System;
public class Animal
{
// Property without the virtual keyword
public string Name { get; set; } = "Animal";
}
public class Dog : Animal
{
// Use the new keyword to redefine the Name property
public new string Name { get; set; } = "Dog";
}
public class Program
{
public static void Main()
{
Animal myAnimal = new Animal();
Console.WriteLine(myAnimal.Name); // Output: Animal
Dog myDog = new Dog();
Console.WriteLine(myDog.Name); // Output: Dog
Animal myAnimalDog = new Dog();
Console.WriteLine(myAnimalDog.Name); // Output: Animal (base class property is called)
}
}In this example:
The
Nameproperty in theAnimalclass is not declared with thevirtualkeyword.In the
Dogclass, thenewkeyword is used to redefine theNameproperty.When accessing the
Nameproperty on a variable of typeAnimalbut referencing aDogobject, theNameproperty of the base classAnimalis called, not that of the derived classDog.
In summary, if you don't use the virtual keyword, you won't be able to override methods, properties, indexers, or events using the override keyword. Instead, you can use the new keyword to redefine them in the derived class. However, this does not change the behavior when calling those members through a base class type variable.
Only using "override" keyword
csharpSao chép mãusing System;
public class Animal
{
public void Speak()
{
Console.WriteLine("Animal speaks");
}
}
public class Dog : Animal
{
public override void Speak() // Error: 'Animal.Speak()' is not marked virtual, abstract, or override
{
Console.WriteLine("Dog barks");
}
}
public class Program
{
public static void Main()
{
Dog myDog = new Dog();
myDog.Speak();
}
}Not using "new" keyword
If you don't use the new keyword and you're not using override, the behavior depends on whether the method, property, indexer, or event in the derived class has the same signature as the member in the base class.
Method without using
overrideornew:
using System;
public class Animal
{
public void Speak()
{
Console.WriteLine("Animal speaks");
}
}
public class Dog : Animal
{
public void Speak() // Without 'override' or 'new'
{
Console.WriteLine("Dog barks");
}
}
public class Program
{
public static void Main()
{
Animal myAnimal = new Animal();
myAnimal.Speak(); // Output: Animal speaks
Dog myDog = new Dog();
myDog.Speak(); // Output: Dog barks
Animal myAnimalDog = new Dog();
myAnimalDog.Speak(); // Output: Dog barks (derived class method is called)
}
}In this example:
The
Speakmethod in theDogclass has the same signature as the one in theAnimalclass (same name and parameters).When calling
Speakon a variable of typeAnimalbut referencing aDogobject, theSpeakmethod of theDogclass is called. This is because the compiler uses the most derived version of the method available at compile time, which is the one in theDogclass.
Property without using
overrideornew:
csharpSao chép mãusing System;
public class Animal
{
public string Name { get; set; } = "Animal";
}
public class Dog: Animal
{
public string Name { get; set; } = "Dog"; // Without 'override' or 'new'
}
public class Program
{
public static void Main()
{
Animal myAnimal = new Animal();
Console.WriteLine(myAnimal.Name); // Output: Animal
Dog myDog = new Dog();
Console.WriteLine(myDog.Name); // Output: Dog
Animal myAnimalDog = new Dog();
Console.WriteLine(myAnimalDog.Name); // Output: Dog (derived class property is called)
}
}Similarly:
The
Nameproperty in theDogclass has the same signature as the one in theAnimalclass.When accessing the
Nameproperty on a variable of typeAnimalbut referencing aDogobject, theNameproperty of theDogclass is called. This is because the compiler uses the most derived version of the property available at compile time, which is the one in theDogclass.
So, without using override or new, if the method, property, indexer, or event in the derived class has the same signature as the member in the base class, it replaces (hides) the member in the base class.
Last updated