What is Inheritance in C#?
๐ก Concept: Inheritance in C#
Inheritance is a fundamental concept in object-oriented programming that allows a class (called a derived or child class) to inherit members (fields, methods, etc.) from another class (called a base or parent class).
๐ Quick Intro
In C#, inheritance allows for reuse and extension of existing code. It promotes modularity and helps avoid duplication by organizing related behaviors in a hierarchy of classes.
๐ง Analogy
Think of a base class as a general blueprint for a vehicle. A car, bike, or truck is a more specialized version of that blueprint, inheriting basic functionality (like moving) while adding their own specific features.
๐ง Technical Explanation
- โ The base class contains common functionality.
- โ
The derived class uses the
:
symbol to inherit from the base. - โ
Inheritance can be used to override methods using
virtual
andoverride
keywords. - โ ๏ธ C# does not support multiple inheritance of classes, but it allows multiple interfaces.
๐ฏ Use Cases
- ๐ Avoid repeating the same code in multiple related classes.
- ๐ Provide common functionality in base classes.
- ๐ง Create extensible class hierarchies in frameworks and libraries.
๐ป Real Code Example
public class Animal
{
public void Eat()
{
Console.WriteLine("Eating...");
}
}
public class Dog : Animal
{
public void Bark()
{
Console.WriteLine("Barking...");
}
}
// Usage
Dog dog = new Dog();
dog.Eat(); // Inherited from Animal
dog.Bark();

โ Interview Q&A
Q1: What is inheritance?
A: It allows a class to inherit members from another class.
Q2: Can C# support multiple inheritance?
A: Not for classes, but yes for interfaces.
Q3: What keyword is used to inherit a class?
A: The colon (:
) symbol.
Q4: What is a base class?
A: A class that provides shared behavior to other classes.
Q5: How do you override a base method?
A: Use virtual
in base and override
in derived class.
Q6: What is the benefit of inheritance?
A: Code reuse, extensibility, and maintainability.
Q7: What is the difference between interface and base class inheritance?
A: Interface defines contract only, base class may contain implementation.
Q8: Can constructors be inherited?
A: No, but the base constructor can be called using base()
.
Q9: What is method hiding?
A: Using new
keyword to hide base method in derived class.
Q10: Can a derived class access private members of base?
A: No, only protected or public members.
๐ MCQs
Q1. Which symbol is used to inherit in C#?
- ->
- :
- =
- inherits
Q2. Which access level allows members to be inherited?
- private
- sealed
- protected
- internal
Q3. What is method overriding?
- Hiding methods
- Renaming methods
- Redefining base method in derived class
- Overloading
Q4. Can constructors be inherited?
- Yes
- Only static ones
- No
- With virtual
Q5. What does 'base' keyword refer to?
- The current class
- Static method
- The parent class
- Global context
Q6. Which keyword enables base method to be overridden?
- sealed
- final
- virtual
- base
Q7. What is the benefit of inheritance?
- Better UI
- Faster builds
- Code reuse
- Multithreading
Q8. What is a derived class?
- Abstract class
- Static class
- Class that inherits another class
- Interface
Q9. Can we override static methods?
- Yes
- Yes, with static
- No
- Only with override
Q10. Which C# feature prevents inheritance?
- private
- readonly
- sealed
- override
๐ก Bonus Insight
Inheritance, when combined with polymorphism and abstraction, creates powerful extensible class hierarchies. It should be used judiciously to avoid tight coupling and deep inheritance chains.
๐ PDF Download
Need a handy summary for your notes? Download this topic as a PDF!