CHAPTER 15
Beginner
Inheritance in C#
Updated: May 17, 2026
5 min read
# CHAPTER 15
Inheritance in C#
1. Introduction
If you are building a game with aWarrior class and a Mage class, both will need an X position, a Y position, and a Health property. Writing these identical properties in both classes violates the DRY (Don't Repeat Yourself) principle. Inheritance solves this by allowing classes to inherit properties and methods from a Parent class.
2. Learning Objectives
By the end of this chapter, you will be able to:- Define Base (Parent) and Derived (Child) classes.
-
Use the
:syntax to establish inheritance.
-
Use the
protectedaccess modifier.
-
Understand the
virtualandoverridekeywords.
-
Prevent inheritance using the
sealedkeyword.
3. Base and Derived Classes
In C#, you inherit from a class using a colon (:).
csharp
4. The protected Access Modifier
We know public is visible everywhere, and private is visible only inside the class.
protected is the middle ground: It is visible inside the class AND inside any derived (child) classes, but hidden from the outside world.
csharp
5. virtual and override (Method Overriding)
What if the Base class has a method, but the Derived class needs to do it *differently*?
You must mark the Base method as virtual (meaning "allow children to change this"), and the Derived method as override (meaning "I am replacing the parent's version").
csharp
6. The base Keyword
Sometimes, you want to override a parent's method, but you *also* want to run the parent's original code. You can call the parent's code using the base keyword.
csharp
7. The sealed Keyword
If you create a highly optimized class and you absolutely do NOT want any other developers inheriting from it and changing its behavior, you mark it as sealed.
csharp
8. OOP and Memory Explanation
When you instantiate aGoblin object, the CLR allocates a single block of memory on the Heap. This block contains the memory footprint for the Goblin properties, AND the memory footprint for all the Enemy properties. It is treated as one unified object in memory.
9. Common Mistakes
- Multiple Inheritance: C++ allows a class to inherit from two parent classes. C# does NOT. A class in C# can only have ONE direct base class. (We use Interfaces to get around this, covered in Chapter 18).
-
Forgetting
virtual: If you try tooverridea method that isn't markedvirtualin the base class, the C# compiler will throw an error.
10. Best Practices
- Favor "Composition over Inheritance". Do not create massive, 7-level deep inheritance chains. If the relationship isn't strictly an "Is-A" relationship (e.g., A Car "Is-A" Vehicle), do not use inheritance.
11. Exercises
-
1.
Create a
Vehiclebase class with aSpeedproperty and aDrive()method. Create aMotorcyclechild class that inherits from it.
-
2.
Make the
Drive()methodvirtual, andoverrideit in theMotorcycleclass to print "Zooming on two wheels!".
12. MCQs with Answers
Question 1
What symbol is used to denote inheritance in C#?
Question 2
A class that inherits from another class is called the:
Question 3
Which access modifier allows visibility to child classes, but hides it from the outside world?
Question 4
To allow a child class to modify a parent's method, the parent method must be marked with:
Question 5
To actually modify the parent's virtual method, the child method must use the keyword:
Question 6
What keyword prevents a class from being inherited by any other class?
Question 8
Which keyword is used to access members or methods of the parent class from within the child class?
Question 9
If Car inherits from Vehicle, what type of relationship is this?
Question 10
Does a derived class inherit the private members of the base class?
13. Interview Questions
-
Q: Explain the difference between
virtual/overrideand method hiding (newkeyword).
- Q: Why does C# not support multiple inheritance for classes?
-
Q: Explain the exact visibility of the
protectedaccess modifier.
14. Summary
Inheritance promotes code reuse by allowing Derived classes to adopt the fields and methods of a Base class. Usingvirtual and override, child classes can customize behaviors, while protected ensures data remains secure but accessible within the family tree.