wasAsked.

Can polymorphism be implemented without using interfaces and inheritance?

PwC · Role not specified · technical

Published

Asked in 2026 · C#

Community answers

Posted by wasAsked editorial collection

Yes. C# supports polymorphism through overloads, generics, delegates, and pattern-based features without requiring a shared interface or base class. Example using a delegate strategy: public static decimal Checkout( decimal subtotal, Func<decimal, decimal> discount) => discount(subtotal); decimal total = Checkout(100m, value => value * 0.90m); Generic code is another form: static T Last<T>(IReadOnlyList<T> items) => items[^1]; These approaches work when behavior can be supplied directly or the algorithm is type-independent. Interfaces remain valuable when multiple types must advertise a stable capability; do not reject them merely to achieve polymorphism another way.

Backbencher Developer · · 0 likes

Yes. Polymorphism means "one interface, many forms" — the same code working with different types. Inheritance and interfaces give subtype (runtime) polymorphism, but C# supports other kinds as well. 1. Method overloading (compile-time / ad hoc polymorphism) public static class Formatter { public static string Format(int value) => value.ToString("N0"); public static string Format(DateTime value) => value.ToString("dd MMM yyyy"); public static string Format(decimal value) => value.ToString("C"); } The compiler picks the method by argument types. Operator overloading is similar: + behaves differently for int, string or a custom Money type. 2. Generics (parametric polymorphism) public static T Max<T>(T a, T b) where T : IComparable<T> => a.CompareTo(b) >= 0 ? a : b; One method works for many types. (The constraint uses an interface here, but generic collections such as List<T> work for any type without constraints.) 3. Delegates and lambdas (behavioural polymorphism without type hierarchies) Func<decimal, decimal> pricing = isPremium ? p => p * 0.9m : p => p; decimal total = pricing(price); Different behaviour is plugged in at runtime — the basis of the strategy pattern without classes. 4. dynamic (late binding / duck typing) dynamic shape = GetShape(); // any object with an Area() method Console.WriteLine(shape.Area()); // resolved at runtime This works without shared interfaces, but you lose compile-time checking and performance, so it is rarely the best choice. 5. Pattern matching (switch over types) string Describe(object o) => o switch { Circle c => $"Circle r={c.Radius}", Square s => $"Square side={s.Side}", _ => "Unknown" }; This gives type-dependent behaviour over closed sets of types, especially with records. Summary answer: "Runtime polymorphism through virtual dispatch requires inheritance or interfaces, but polymorphism in a broader sense is also achieved with overloading, generics, delegates, dynamic and pattern matching. In real projects I mostly use interfaces for dependency injection, generics for reusable code, and delegates for small strategies."

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics