What is Polymorphism in C#?

πŸ’‘ Concept: Polymorphism in C#

Polymorphism is a core concept in object-oriented programming that allows objects to be treated as instances of their parent class, while invoking the overridden or overloaded behavior at runtime or compile time respectively.

πŸ“˜ Quick Intro

In C#, polymorphism enables code flexibility and extensibility. There are two main types: compile-time (method overloading) and run-time (method overriding via inheritance).

🧠 Analogy

Think of a remote control. Regardless of whether it controls a TV, AC, or speaker, pressing the "Power" button performs the appropriate action. Polymorphism is like that β€” the same interface triggers different results depending on the object.

πŸ”§ Technical Explanation

  • πŸ” Compile-time polymorphism uses method overloading (same method name, different parameters).
  • 🧩 Run-time polymorphism is achieved by using virtual and override keywords with inheritance.
  • 🧠 Base class references can call overridden methods in derived classes using dynamic dispatch.
  • ⚠️ Polymorphism requires the use of inheritance or interfaces for run-time flexibility.

🎯 Use Cases

  • πŸ“¦ Designing APIs and libraries that work with general types but allow specific behavior.
  • ♻️ Replacing long if-else or switch conditions with object behavior.
  • πŸ“ˆ Making systems more extensible and easier to maintain.

πŸ’» Real Code Example

// Compile-time Polymorphism (Overloading)
public class Calculator {
    public int Add(int a, int b) => a + b;
    public double Add(double a, double b) => a + b;
}

// Run-time Polymorphism (Overriding)
public class Animal {
    public virtual void Speak() => Console.WriteLine("Animal speaks");
}

public class Dog : Animal {
    public override void Speak() => Console.WriteLine("Dog barks");
}

Animal pet = new Dog();
pet.Speak(); // Outputs: Dog barks

❓ Interview Q&A

Q1: What is polymorphism in C#?
A: It allows methods to behave differently based on the object context.

Q2: What is compile-time polymorphism?
A: It's method overloading resolved during compilation.

Q3: What is run-time polymorphism?
A: It’s method overriding resolved during program execution.

Q4: What keyword enables overriding in C#?
A: virtual in base and override in derived class.

Q5: Can constructors be overloaded?
A: Yes, constructors support overloading.

Q6: What is method hiding?
A: It occurs when a derived class defines a method with the same name and uses the new keyword.

Q7: Is method overloading polymorphism?
A: Yes, it is compile-time polymorphism.

Q8: Can private methods be overridden?
A: No, only accessible and virtual methods can be overridden.

Q9: Why use polymorphism?
A: To enable extensibility and code reuse.

Q10: Can interfaces support polymorphism?
A: Yes, interfaces enable polymorphism by allowing different implementations.

πŸ“ MCQs

Q1. What is compile-time polymorphism?

  • Method overriding
  • Method hiding
  • Method overloading
  • Inheritance

Q2. Which keyword enables runtime polymorphism?

  • const
  • new
  • override
  • virtual

Q3. What is the result of method overriding?

  • Compile error
  • Static dispatch
  • Dynamic behavior at runtime
  • Interface implementation

Q4. Which is NOT a form of polymorphism?

  • Method overloading
  • Method overriding
  • Method serialization
  • Virtual dispatch

Q5. Which access modifier is needed for polymorphism?

  • private
  • sealed
  • public/protected
  • internal

Q6. Which C# keyword hides a base method?

  • static
  • base
  • new
  • override

Q7. Which method call supports late binding?

  • Overloaded method
  • Static method
  • Overridden method
  • Sealed method

Q8. What’s the advantage of polymorphism?

  • Shorter code
  • Less memory
  • Faster runtime
  • Flexibility and extensibility

Q9. Can polymorphism be achieved via interfaces?

  • No
  • Yes
  • Only with abstract classes
  • Only in Java

Q10. What is dynamic dispatch?

  • Call to dynamic keyword
  • Method call determined at compile time
  • Method call determined at runtime
  • Compiler optimization

πŸ’‘ Bonus Insight

Polymorphism makes your code flexible and future-proof. By programming to interfaces or base types, you allow extensions without modifying existing logic β€” a key principle of the Open/Closed principle in SOLID design.

πŸ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

πŸ” Navigation

πŸ’¬ Feedback
πŸš€ Start Learning
Share:

Tags: