wasAsked.

Any other way to implement overriding other than using 'virtual' and 'override'

HCA Healthcare · Role not specified · technical

Published

Year not specified · C#

Community answers

Posted by wasAsked editorial collection

True runtime overriding requires a virtual, abstract, or already-overridden base member plus override in the derived class. Interfaces provide runtime polymorphism without overriding a class member. The new keyword performs hiding, not overriding. Example: public interface IPriceRule { decimal Apply(decimal value); } public sealed class TaxRule : IPriceRule { public decimal Apply(decimal value) => value * 1.18m; } IPriceRule rule = new TaxRule(); Use interfaces or composition when types share a capability rather than a genuine is-a relationship. Avoid presenting method hiding as overriding because dispatch can depend on the reference's compile-time type and surprise callers.

Backbencher Developer · · 0 likes

In C#, true overriding (runtime polymorphism through a base-class reference) requires the base member to be virtual, abstract or itself an override, and the derived member to use the override keyword. But there are several other ways to change or customise behaviour, which is probably what the interviewer wants to hear. 1. Abstract members An abstract method in an abstract class is implicitly virtual; derived classes implement it with override, without the virtual keyword appearing anywhere. public abstract class Report { public abstract string Render(); } public class PdfReport : Report { public override string Render() => "PDF"; } 2. Interface implementation Different classes implement the same interface method differently; calls through the interface are dispatched at runtime — polymorphism without virtual and override. public interface IExporter { string Export(Data d); } public class CsvExporter : IExporter { public string Export(Data d) => "csv"; } public class JsonExporter : IExporter { public string Export(Data d) => "json"; } Default interface methods (C# 8+) can be re-implemented by classes in a similar way. 3. Method hiding with new (not real overriding) public class Base { public void Show() => Console.WriteLine("Base"); } public class Derived : Base { public new void Show() => Console.WriteLine("Derived"); } Base b = new Derived(); b.Show(); // prints "Base" — chosen at compile time by the reference type This hides the base method instead of overriding it, so it does not give runtime polymorphism. It is mainly useful when a base class you do not control later adds a member with the same name. 4. Delegates and strategy objects Inject behaviour instead of inheriting it: public class PriceCalculator(Func<decimal, decimal> discountRule) { public decimal Final(decimal price) => discountRule(price); } 5. sealed override Not an alternative, but related: sealed override stops further overriding in deeper derived classes. Summary: genuine overriding needs virtual/abstract plus override; the alternatives that achieve polymorphic behaviour are abstract members, interfaces and composition (delegates or strategy pattern), while new only hides methods.

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics