C#  

Understanding Inheritance in C# WebForms

Introduction

Inheritance is one of the core pillars of Object-Oriented Programming (OOP) in C#.
It allows one class (child class) to reuse and extend the properties and methods of another class (base class).

In this example, we’ll create:

  • A base classEmployee

  • A derived classManager that inherits from Employee
    Then, we’ll display employee and manager details in an ASP.NET WebForms page.

Concept Overview

ConceptDescription
Base Class (Parent)Contains common properties or methods shared by all derived classes.
Derived Class (Child)Inherits members from the base class and can add its own members.
Keyword: baseClassName is used for inheritance.

Real-Time Example: Employee → Manager

Step 1: ASPX Page (InheritanceDemo.aspx)

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="InheritanceDemo.aspx.cs" Inherits="WebApp.InheritanceDemo" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Inheritance Example - Employee → Manager</title>
</head>
<body>
    <form id="form1" runat="server">
        <div style="font-family: Arial; margin: 40px;">
            <h2>Inheritance Example (Employee → Manager)</h2>

            <asp:Button ID="btnShow" runat="server" Text="Show Details" OnClick="btnShow_Click" /><br /><br />

            <asp:Label ID="lblResult" runat="server" Font-Names="Consolas" Font-Size="Large"></asp:Label>
        </div>
    </form>
</body>
</html>

Step 2: Code-Behind (InheritanceDemo.aspx.cs)

using System;

namespace WebApp
{
    public partial class InheritanceDemo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnShow_Click(object sender, EventArgs e)
        {
            // Create object of Manager class (derived class)
            Manager mgr = new Manager();
            mgr.EmpId = 101;
            mgr.Name = "Sandhiya";
            mgr.Designation = "Manager";
            mgr.Department = "IT";
            mgr.Bonus = 15000;

            // Display details
            lblResult.Text = mgr.DisplayDetails();
        }
    }

    // Base class
    public class Employee
    {
        public int EmpId { get; set; }
        public string Name { get; set; }
        public string Designation { get; set; }

        public virtual string DisplayDetails()
        {
            return $"Employee ID: {EmpId}<br/>" +
                   $"Name: {Name}<br/>" +
                   $"Designation: {Designation}<br/>";
        }
    }

    // Derived class inheriting from Employee
    public class Manager : Employee
    {
        public string Department { get; set; }
        public double Bonus { get; set; }

        // Method overriding
        public override string DisplayDetails()
        {
            string baseDetails = base.DisplayDetails();
            return baseDetails +
                   $"Department: {Department}<br/>" +
                   $"Bonus: ₹{Bonus}<br/>";
        }
    }
}

Explanation

  1. Base Class → Employee

    • Contains general properties like EmpId, Name, and Designation.

    • Includes a virtual method DisplayDetails().

  2. Derived Class → Manager

    • Inherits from Employee using class Manager : Employee.

    • Adds new properties Department and Bonus.

    • Uses method overriding (override) to modify how details are displayed.

  3. WebForms Page

    • When the button is clicked, a Manager object is created.

    • The manager’s details (inherited + new fields) are shown in the label.

Output

After clicking “Show Details”, the page displays:

Employee ID: 101
Name: Sandhiya
Designation: Manager
Department: IT
Bonus: ₹15000

Key OOP Concepts Used

ConceptDescription
InheritanceEnables a class to reuse another class’s members.
Base KeywordCalls the base class’s method or constructor.
Method OverridingAllows derived classes to change the behavior of inherited methods.
EncapsulationProperties protect data with getters and setters.

Real-Time Use Case

Inheritance is used in:

  • Company HR Systems: (Employee → Manager → Director)

  • Educational Portals: (Person → Student → GraduateStudent)

  • Banking Applications: (Account → SavingsAccount → CurrentAccount)

Conclusion

This example demonstrates how inheritance works in real-world C# WebForms applications.

By extending the Employee class into a Manager, we achieve code reusability, clean design, and modular logic—all of which are essential for scalable enterprise applications.