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>
Code Behind (InheritanceExample.aspx.cs)
using System;
namespace WebFormsDemo
{
// Base class
public class Employee
{
public string Name { get; set; }
public virtual string DisplayRole()
{
return "Employee: Works in the organization.";
}
}
// Derived classes
public class Manager : Employee
{
public override string DisplayRole()
{
return "Manager: Oversees team goals and performance.";
}
}
public class Developer : Employee
{
public override string DisplayRole()
{
return "Developer: Builds and maintains applications.";
}
}
public class Tester : Employee
{
public override string DisplayRole()
{
return "Tester: Ensures software quality and bug-free delivery.";
}
}
public partial class InheritanceExample : System.Web.UI.Page
{
protected void btnShow_Click(object sender, EventArgs e)
{
string selectedRole = ddlRole.SelectedValue;
Employee emp = null;
switch (selectedRole)
{
case "Manager":
emp = new Manager();
break;
case "Developer":
emp = new Developer();
break;
case "Tester":
emp = new Tester();
break;
default:
lblResult.Text = "Please select a valid role.";
return;
}
lblResult.Text = emp.DisplayRole();
}
}
}
Output Example
| Selected Role | Output |
|---|---|
| Manager | Manager: Oversees team goals and performance. |
| Developer | Developer: Builds and maintains applications. |
| Tester | Tester: Ensures software quality and bug-free delivery. |
4. Real-Time Use Cases in Enterprise Projects
| Scenario | Inheritance Use | Polymorphism Use |
|---|---|---|
| Role-based dashboard | Base: User, Derived: Admin, Manager, Employee | Display different UI or permissions |
| Payment Gateway | Base: Payment, Derived: CardPayment, UPIPayment | Execute specific payment logic dynamically |
| Notification System | Base: Notification, Derived: Email, SMS, Push | Trigger different send mechanisms |
| E-commerce Orders | Base: Order, Derived: OnlineOrder, StorePickupOrder | Override delivery or billing logic |
Key Takeaways
Inheritance → Enables code reusability
Polymorphism → Enables flexibility and extensibility
Real-life projects → Use both together for scalable architecture
Conclusion
By mastering Inheritance and Polymorphism, you write cleaner and more maintainable C# code.
In ASP.NET WebForms, these concepts are invaluable for role-based features, modular architecture, and reusable logic across pages.

Join the conversation! Your thoughts help the community grow.