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 and override 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!

๐Ÿ” Navigation

Learn More About C# ๐Ÿ“š

1. What is C#? ๐Ÿ‘‰ Explained
2. Main Features of C# ๐Ÿ‘‰ Explained
3. Difference Between C# and Java ๐Ÿ‘‰ Explained
4. Common Language Runtime (CLR) in C# ๐Ÿ‘‰ Explained
5. Common Type System (CTS) in C# ๐Ÿ‘‰ Explained
6. Common Language Specification (CLS) in C# ๐Ÿ‘‰ Explained
7. Value Types vs Reference Types in C# ๐Ÿ‘‰ Explained
8. What is a Namespace in C#? ๐Ÿ‘‰ Explained
9. Purpose of the 'using' Keyword in C# ๐Ÿ‘‰ Explained
10. Different Data Types in C# ๐Ÿ‘‰ Explained
11. Difference Between int and Int32 in C# ๐Ÿ‘‰ Explained
12. Difference Between float, double, and decimal in C# ๐Ÿ‘‰ Explained
13. What is the Default Value of a Boolean in C#? ๐Ÿ‘‰ Explained
14. What is Boxing and Unboxing in C# ๐Ÿ‘‰ Explained
15. What are the Different Types of Operators in C# ๐Ÿ‘‰ Explained
16. Difference Between Equals and == in C# ๐Ÿ‘‰ Explained
17. What is the Null-Coalescing Operator ?? in C# ๐Ÿ‘‰ Explained
18. What is the Ternary Operator in C# ๐Ÿ‘‰ Explained
19. How Does the Switch Statement Work in C# ๐Ÿ‘‰ Explained
20. What is Object-Oriented Programming in C# ๐Ÿ‘‰ Explained
21. What are the Four Pillars of OOP in C# ๐Ÿ‘‰ Explained
22. What is Encapsulation in C# ๐Ÿ‘‰ Explained
23. What is Inheritance in C# ๐Ÿ‘‰ Explained
24. What is Polymorphism in C# ๐Ÿ‘‰ Explained
25. What is Abstraction in C# ๐Ÿ‘‰ Explained
26. What is an Abstract Class in C# ๐Ÿ‘‰ Explained
27. What is an Interface in C# ๐Ÿ‘‰ Explained
28. Can a Class Implement Multiple Interfaces in C#? ๐Ÿ‘‰ Explained
29. Difference Between Abstract Class and Interface in C# ๐Ÿ‘‰ Explained
30. How Do You Create a Class in C#? ๐Ÿ‘‰ Explained
31. What is a Constructor in C# ๐Ÿ‘‰ Explained
32. What Are the Types of Constructors in C# ๐Ÿ‘‰ Explained
33. What is a Static Constructor in C# ๐Ÿ‘‰ Explained
34. Difference Between Static and Non-Static Members in C# ๐Ÿ‘‰ Explained
35. What is the Use of 'this' Keyword in C# ๐Ÿ‘‰ Explained
36. What is a Destructor in C# ๐Ÿ‘‰ Explained
37. What is Object Initializer Syntax in C# ๐Ÿ‘‰ Explained
38. What is the Difference Between Field and Property in C# ๐Ÿ‘‰ Explained
Share:

Tags:


Feedback Modal Popup