override keyword is used to provide a new implementation of a member inherited from a base method.
class Demo { static void Main(string[] args) { Animal a = new Cat(); Animal b = new Dog(); Animal c = new Animal(); a.sayHi(); b.sayHi(); c.sayHi(); Console.ReadLine(); } } class Animal { public Animal() { Console.WriteLine("Animal"); } public virtual void sayHi() { Console.WriteLine("Animal sayHi"); } } class Cat : Animal { public Cat() { Console.WriteLine("Cat"); } public new void sayHi() { Console.WriteLine("Cat sayHi"); } } class Dog : Animal { public Dog() { Console.WriteLine("Dog"); } public override void sayHi() { Console.WriteLine("Dog sayHi"); } }
The result is shown below:
No comments:
Post a Comment