Not for the same method signature. A class can declare a given method only once, so the same member in one class cannot be both an override and a hidden (new) method. But within a single derived class you can override some inherited members and hide others, and you can even combine new with virtual. Override one method and hide another in the same class: public class Animal { public virtual string Speak() => "..."; public string Describe() => "An animal"; // not virtual } public class Dog : Animal { public override string Speak() => "Woof"; // overriding: runtime polymorphism public new string Describe() => "A dog"; // hiding: compile-time binding } Animal a = new Dog(); Console.WriteLine(a.Speak()); // "Woof" — override is used Console.WriteLine(a.Describe()); // "An animal" — base method, because hiding depends on the reference type Dog d = new Dog(); Console.WriteLine(d.Describe()); // "A dog" Starting a new virtual chain with new virtual: public class Puppy : Dog { public new virtual string Speak() => "Yip"; // hides Dog.Speak and starts a new virtual slot } Animal x = new Puppy(); Console.WriteLine(x.Speak()); // "Woof" — the Animal chain still ends at Dog.Speak Dog y = new Puppy(); Console.WriteLine(y.Speak()); // "Woof" Puppy z = new Puppy(); Console.WriteLine(z.Speak()); // "Yip" So yes, hiding and overriding can both appear in one class, but for different members (or one member starting a new virtual chain with new virtual). This is legal yet confusing, so it should be rare in real code. Key differences to state - override replaces the base implementation for all references (base or derived) — dynamic dispatch. - new hides the base member only when accessed through the derived type — static dispatch. - Without new, the compiler warns (CS0108) that you are hiding a member.
Ravindra Wadile · · 0 likes