Introduction
C# is an object-oriented language, which means it supports features like Encapsulation, Inheritance, and Polymorphism.
These features make your code reusable, maintainable, and scalable — especially in enterprise ASP.NET applications.
In this article, we’ll focus on:
What is Inheritance
What is Polymorphism
Why do we use them in real-time projects
Practical ASP.NET WebForms example
1. What Is Inheritance?
Inheritance is the process by which one class (child or derived class) inherits the properties and methods of another class (parent or base class).
It helps in:
Code reusability
Reducing duplication
Improving scalability
Example (Simple)
class Employee
{
public string Name;
public double Salary;
public void ShowDetails()
{
Console.WriteLine($"Name: {Name}, Salary: {Salary}");
}
}
class Manager : Employee
{
public string Department;
public void ShowDepartment()
{
Console.WriteLine($"Department: {Department}");
}
}
Here, Manager inherits all properties and methods from Employee.
So, you can access Name, Salary, and ShowDetails() directly from the Manager object.
2. What Is Polymorphism?
Polymorphism means “many forms”.
In C#, it allows the same method name to behave differently based on the object that calls it.
There are two types:
Compile-time Polymorphism (Method Overloading)
Run-time Polymorphism (Method Overriding using
virtualandoverride)
Example 1: Compile-time Polymorphism (Method Overloading)
class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
public double Add(double a, double b)
{
return a + b;
}
}
Here, both methods are named Add, but have different parameter types.
The compiler decides which one to call based on input — that’s compile-time polymorphism.
Example 2: Run-time Polymorphism (Method Overriding)
class Employee
{
public virtual void DisplayRole()
{
Console.WriteLine("Employee: General Role");
}
}
class Manager : Employee
{
public override void DisplayRole()
{
Console.WriteLine("Manager: Oversees team performance");
}
}
class Developer : Employee
{
public override void DisplayRole()
{
Console.WriteLine("Developer: Writes and tests code");
}
}
Here, the method DisplayRole() is overridden in each subclass.
Which version runs depends on the object type at runtime.
3. Real-Time Example in ASP.NET WebForms
Let’s see a working example in a web application.
We’ll build a simple page that displays different employee roles dynamically.
ASPX Page (InheritanceExample.aspx)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="InheritanceExample.aspx.cs" Inherits="WebFormsDemo.InheritanceExample" %>
<!DOCTYPE html>
<html>
<head>
<title>Inheritance and Polymorphism in ASP.NET C#</title>
</head>
<body>
<h2>Understanding Inheritance and Polymorphism in C#</h2>
<asp:DropDownList ID="ddlRole" runat="server">
<asp:ListItem Text="Select Role" Value=""></asp:ListItem>
<asp:ListItem Text="Manager" Value="Manager"></asp:ListItem>
<asp:ListItem Text="Developer" Value="Developer"></asp:ListItem>
<asp:ListItem Text="Tester" Value="Tester"></asp:ListItem>
</asp:DropDownList>
<asp:Button ID="btnShow" runat="server" Text="Show Role Description" OnClick="btnShow_Click" />
<br /><br />
<asp:Label ID="lblResult" runat="server" ForeColor="Blue"></asp:Label>
</body>
</html>

Join the conversation! Your thoughts help the community grow.