1. 🔴 What is polymorphism, and what are its types in C#?
Polymorphism is one of the four pillars of OOP. It means one method or interface can have multiple forms of behavior.
In C#, polymorphism allows the same method name or method call to behave differently depending on the parameters or actual object type.
There are two main types:
-
Compile-time polymorphism (Static Binding) – Achieved through method overloading and operator overloading.
-
Runtime polymorphism (Dynamic Binding) – Achieved through method overriding using
virtualandoverride.
Example:
// Compile-time polymorphism
class Calculator
{
public int Add(int a, int b) => a + b;
public double Add(double a, double b) => a + b;
}
// Runtime polymorphism
class Animal
{
public virtual void Speak() => Console.WriteLine("Animal");
}
class Dog : Animal
{
public override void Speak() => Console.WriteLine("Bark");
}
Key point: Compile-time polymorphism selects a method based on its signature; runtime polymorphism selects an overridden implementation based on the actual object type.
2. 🔴 What is the difference between compile-time and runtime polymorphism?
|
Compile-time polymorphism |
Runtime polymorphism |
|---|---|
|
Method selected at compile time |
Implementation selected at runtime |
|
Also called static binding |
Also called dynamic binding |
|
Achieved through overloading |
Achieved through overriding |
|
Inheritance not required |
Requires inheritance for class overrides |
|
Uses different parameter signatures |
Uses compatible overriding signatures |
|
Does not require |
Uses |
Example:
// Compile-time
calculator.Add(10, 20);
calculator.Add(10.5, 20.5);
// Runtime
Animal animal = new Dog();
animal.Speak(); // Bark
Remember: Overloading → Compile Time; Overriding → Runtime.
3. 🔴 What is method overloading? Explain its rules with examples.
Method overloading means defining multiple methods with the same name but different parameter signatures within a class.
The compiler determines which method to execute based on the supplied arguments.
Example:
class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
public int Add(int a, int b, int c)
{
return a + b + c;
}
public double Add(double a, double b)
{
return a + b;
}
}
Usage:
Calculator calc = new Calculator();
Console.WriteLine(calc.Add(10, 20)); // 30
Console.WriteLine(calc.Add(10, 20, 30)); // 60
Console.WriteLine(calc.Add(5.5, 4.5)); // 10
Rules of method overloading:
-
Method names must be the same.
-
The number of parameters can differ.
-
Parameter types can differ.
-
Parameter order can differ when the types differ.
-
Changing only the return type is not allowed.
-
staticand instance methods can both be overloaded, butstaticalone cannot distinguish signatures.
Key point: Method overloading improves code readability and reusability.
4. 🔴 What is method overriding? Explain using virtual and override.
Method overriding allows a derived class to provide its own implementation of an inherited virtual method.
-
virtual– Allows the base class method to be overridden. -
override– Provides the derived class implementation.
Example:
class Employee
{
public virtual void CalculateSalary()
{
Console.WriteLine("Basic Salary");
}
}
class Manager : Employee
{
public override void CalculateSalary()
{
Console.WriteLine("Salary + Bonus");
}
}
Usage:
Employee emp = new Manager();
emp.CalculateSalary();
Output:
Salary + Bonus
Important rules:
-
The base method must be
virtual,abstract, oroverride. -
The derived method must use
override. -
Method signatures must be compatible.
-
Private and static methods cannot be overridden.
-
An overridden method can call the base implementation using
base.MethodName().
Key point: Overriding supports runtime polymorphism and allows specialized behavior in derived classes.
5. 🔴 What is the difference between method overloading and method overriding?
|
Method overloading |
Method overriding |
|---|---|
|
Same method name |
Same method name |
|
Different parameters |
Same parameter signature |
|
Compile-time polymorphism |
Runtime polymorphism |
|
Inheritance not required |
Inheritance required |
|
No |
|
|
Different implementations selected by arguments |
Implementation selected by runtime object type |
Example:
// Overloading
void Print(int number) { }
void Print(string text) { }
// Overriding
class Parent
{
public virtual void Print() { }
}
class Child : Parent
{
public override void Print() { }
}
Remember: Overloading changes the parameter list; overriding changes inherited behavior.
6. 🔴 What happens when a base class reference points to a derived class object?
This is called upcasting and is commonly used to achieve runtime polymorphism.
A base class reference can hold an object of its derived class.
Example:
class Animal
{
public virtual void Speak()
{
Console.WriteLine("Animal Sound");
}
}
class Dog : Animal
{
public override void Speak()
{
Console.WriteLine("Dog Barks");
}
public void Run()
{
Console.WriteLine("Dog Running");
}
}
Usage:
Animal obj = new Dog();
obj.Speak(); // Dog Barks
// obj.Run(); // Compile-time error
Explanation:
-
Reference type:
Animal -
Actual object type:
Dog -
Overridden method executed:
Dog.Speak() -
Only members accessible through the
Animaltype can be called directly.
To access Run():
Dog dog = (Dog)obj;
dog.Run();
Key point: The reference type controls accessible members; the actual object type determines which overridden method executes.
7. 🔴 What is runtime method dispatch, and how does C# determine which overridden method to execute?
Runtime method dispatch is the process of selecting the appropriate overridden method based on the actual object type during execution.
It is also called dynamic method dispatch.
Example:
class Animal
{
public virtual void Speak()
{
Console.WriteLine("Animal");
}
}
class Dog : Animal
{
public override void Speak()
{
Console.WriteLine("Dog");
}
}
class Cat : Animal
{
public override void Speak()
{
Console.WriteLine("Cat");
}
}
Usage:
Animal a1 = new Dog();
Animal a2 = new Cat();
a1.Speak(); // Dog
a2.Speak(); // Cat
Execution flow:
Animal a1 = new Dog()
|
v
a1.Speak()
|
v
Runtime checks actual object type
|
v
Object Type = Dog
|
v
Dog.Speak() executed
Key point: C# uses virtual method dispatch to resolve the correct override at runtime, rather than relying only on the reference type.
8. 🟡 Can methods be overloaded by changing only their return types?
No. C# does not allow method overloading based only on the return type.
Invalid example:
class Calculator
{
public int Add(int a, int b) => a + b;
// Compilation error
public double Add(int a, int b) => a + b;
}
Both methods have identical parameter signatures, so the compiler cannot distinguish them.
Valid example:
public int Add(int a, int b) => a + b;
public double Add(double a, double b) => a + b;
Key point: Return type alone is not part of the method-overload signature.
9. 🟡 Can static methods be overloaded or overridden?
Static methods can be overloaded, but they cannot be overridden.
Static methods belong to the class rather than an object instance.
Static method overloading:
class Calculator
{
public static int Add(int a, int b) => a + b;
public static double Add(double a, double b) => a + b;
}
Usage:
Calculator.Add(10, 20);
Calculator.Add(10.5, 20.5);
Static method hiding:
class Parent
{
public static void Show()
{
Console.WriteLine("Parent");
}
}
class Child : Parent
{
public new static void Show()
{
Console.WriteLine("Child");
}
}
Usage:
Parent.Show(); // Parent
Child.Show(); // Child
Key point: Static methods support overloading and hiding, but not runtime overriding.
10. 🟡 What is the difference between operator overloading and method overloading?
Method overloading allows multiple methods with the same name but different parameters.
Operator overloading allows operators such as +, -, and == to have customized behavior for user-defined types.
Method overloading:
int Add(int a, int b) => a + b;
double Add(double a, double b) => a + b;
Operator overloading:
class Number
{
public int Value { get; set; }
public static Number operator +(Number a, Number b)
{
return new Number
{
Value = a.Value + b.Value
};
}
}
Usage:
Number n1 = new Number { Value = 10 };
Number n2 = new Number { Value = 20 };
Number result = n1 + n2;
Console.WriteLine(result.Value); // 30
|
Method overloading |
Operator overloading |
|---|---|
|
Overloads methods |
Overloads operators |
|
Same method name |
Uses |
|
Different parameter signatures |
Custom operator behavior |
|
Called using method syntax |
Called using operator syntax |
Key point: Both are forms of compile-time polymorphism.
11. 🟡 What is the difference between overriding and method hiding using new?
Overriding replaces the inherited virtual method implementation for runtime dispatch.
Method hiding creates a new member in the derived class that hides the inherited member when accessed through the derived type.
Example:
class Parent
{
public virtual void Show()
{
Console.WriteLine("Parent");
}
}
class Child : Parent
{
public new void Show()
{
Console.WriteLine("Child");
}
}
Usage:
Parent obj1 = new Child();
obj1.Show(); // Parent
Child obj2 = new Child();
obj2.Show(); // Child
Now change new to override:
class Child : Parent
{
public override void Show()
{
Console.WriteLine("Child");
}
}
Usage:
Parent obj1 = new Child();
obj1.Show(); // Child
Child obj2 = new Child();
obj2.Show(); // Child
Comparison:
|
|
|
|---|---|
|
Overrides inherited virtual behavior |
Hides inherited member |
|
Supports runtime polymorphism |
Does not override base virtual dispatch |
|
Requires an overridable base method |
Can hide a non-virtual method |
|
Actual object type determines override |
Reference type affects member selection |
|
Uses |
Uses |
Key point: Use override when you want polymorphic behavior. Use new when you intentionally want to hide an inherited member.
Quick Interview Revision – Most Important Points
|
Concept |
Remember |
|---|---|
|
Polymorphism |
One interface, multiple behaviors |
|
Compile-time polymorphism |
Method/operator overloading |
|
Runtime polymorphism |
Method overriding |
|
Overloading |
Same name, different parameters |
|
Overriding |
Inherited virtual method, new implementation |
|
|
Allows overriding |
|
|
Implements an override |
|
|
Hides an inherited member |
|
|
Accesses base class implementation |
|
Static methods |
Overload: Yes; Override: No |
|
Return type only |
Cannot overload |
|
Base reference, derived object |
Runtime override is executed |
|
Runtime dispatch |
Uses actual object type |
Interview priority: Questions 3, 4, 5, 6, 7, and 11 are especially important for Senior .NET Developer and Technical Lead interviews because they test practical understanding of inheritance, method resolution, and runtime behavior.