Master C# Inheritance & Polymorphism: Practical Code Examples
What Is Inheritance in C#?
Inheritance is a cornerstone of C# object‑oriented design. It lets a child class automatically receive the fields and methods of a parent class, while still permitting the child to override or extend that behavior. This promotes code reuse, simplifies maintenance, and establishes a clear hierarchy.
- Defines parent and child classes.
- Child inherits parent members.
- Child can modify inherited behavior.
- Child can add its own members.
C# Inheritance Example
Below is a step‑by‑step demonstration that shows how to implement inheritance and use protected members.
Step 1 – Modify the Base Class
Open Tutorial.cs and change the Tutorial class so its key fields are protected:

Using protected ensures that only derived classes can access these fields directly.
Step 2 – Create a Derived Class
Append the following code to Tutorial.cs after the base class definition:

Explanation:
- Declare
Guru99Tutorialas a child ofTutorialusing the colon (:) syntax. - Add a method
RenameTutorialthat assigns a new value toTutorialName. - Because
TutorialNameis protected,Guru99Tutorialcan access it even though it isn’t explicitly declared there.
Step 3 – Use the Derived Class in Program.cs
Replace the contents of Program.cs with the following:

using System;
namespace DemoApplication
{
public class Tutorial
{
protected int TutorialID;
protected string TutorialName;
public void SetTutorial(int pID, string pName)
{
TutorialID = pID;
TutorialName = pName;
}
public string GetTutorial()
{
return TutorialName;
}
}
public class Guru99Tutorial : Tutorial
{
public void RenameTutorial(string pNewName)
{
TutorialName = pNewName;
}
static void Main(string[] args)
{
Guru99Tutorial pTutor = new Guru99Tutorial();
pTutor.RenameTutorial(".Net by Guru99");
Console.WriteLine(pTutor.GetTutorial());
Console.ReadKey();
}
}
}
Explanation:
- The program instantiates
Guru99Tutorial, not the baseTutorial. - It calls
RenameTutorialto change the title. - Even though
GetTutorialis defined in the base class, the derived instance can invoke it directly.
Running this code prints:

What Is Polymorphism in C#?
Polymorphism lets a single interface represent different underlying forms. In C#, this often manifests as method overloading or overriding, allowing the same method name to perform varied tasks based on parameters or context.
C# Polymorphism Example
We’ll demonstrate method overloading within the same class.
Step 1 – Update the Base Class
Modify Tutorial.cs as shown:

Key changes:
- The
SetTutorialmethod now has two overloads – one that accepts anintandstring, and another that accepts only astring. - Both overloads assign values to the corresponding fields.
Step 2 – Test in Program.cs
Replace Program.cs with the following code:
using System;
namespace DemoApplication
{
class Tutorial
{
public int TutorialID;
public string TutorialName;
public void SetTutorial(int pID, string pName)
{
TutorialID = pID;
TutorialName = pName;
}
public void SetTutorial(string pName)
{
TutorialName = pName;
}
public string GetTutorial()
{
return TutorialName;
}
static void Main(string[] args)
{
Tutorial pTutor = new Tutorial();
pTutor.SetTutorial(1, "First Tutorial");
Console.WriteLine(pTutor.GetTutorial());
pTutor.SetTutorial("Second Tutorial");
Console.WriteLine(pTutor.GetTutorial());
Console.ReadKey();
}
}
}
Explanation:
- First call passes both
idandnameto the two‑parameter overload. - Second call passes only
name, invoking the single‑parameter overload.
The console displays:

For advanced scenarios, you can add a GetTutorialID method to retrieve the ID separately.
Summary
- Inheritance allows a child class to inherit and extend parent members.
- Polymorphism, especially method overloading, lets a single method name adapt to different parameter sets.
- Both concepts are essential for clean, maintainable, and reusable C# code.
C Language
- C# Expressions, Statements & Blocks: A Comprehensive Guide with Practical Examples
- C++ Inheritance Models: Multiple, Multilevel, Hierarchical
- C++ Classes & Objects: A Practical Guide with Code Examples
- C++ Functions Explained with Practical Code Examples
- Master C# Classes & Objects: A Practical Tutorial with Real‑World Examples
- Understanding C# Access Modifiers (Specifiers) with Practical Examples
- Python OOP Fundamentals: Classes, Objects, Inheritance, and Constructors Explained
- Understanding type() and isinstance() in Python: Practical Examples
- Mastering C# Inheritance: Build Reusable, Maintainable Code
- C# Polymorphism: Static vs Dynamic Binding Explained