C#  

Understanding Method Overloading and Overriding in C# WebForms

Introduction

In Object-Oriented Programming (OOP), methods can share the same name but behave differently based on their parameters or class hierarchy.
This concept is achieved through:

  1. Method Overloading – Compile-time polymorphism

  2. Method Overriding – Run-time polymorphism

We’ll create a C# WebForms application that demonstrates both concepts in a practical way.

Objective

  • To show method overloading (same method name, different parameters).

  • To show method overriding (same method name in parent and child class).

  • To display the results on a WebForm.

C# WebForms Real-Time Example

Step 1: ASPX Page (OverloadingOverridingDemo.aspx)

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

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Method Overloading and Overriding Example</title>
</head>
<body>
    <form id="form1" runat="server">
        <div style="font-family: Arial; margin: 50px;">
            <h2>Method Overloading & Overriding Example</h2>

            <asp:Button ID="btnShow" runat="server" Text="Run Example" 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 (OverloadingOverridingDemo.aspx.cs)

using System;

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

        protected void btnShow_Click(object sender, EventArgs e)
        {
            string result = "";

            // Demonstrate Method Overloading
            Calculator calc = new Calculator();
            result += "<b>Method Overloading:</b><br/>";
            result += "Add(10, 20) = " + calc.Add(10, 20) + "<br/>";
            result += "Add(10.5, 20.5) = " + calc.Add(10.5, 20.5) + "<br/>";
            result += "Add(10, 20, 30) = " + calc.Add(10, 20, 30) + "<br/><br/>";

            // Demonstrate Method Overriding
            Animal a1 = new Animal();
            Animal a2 = new Dog();

            result += "<b>Method Overriding:</b><br/>";
            result += "Animal Sound: " + a1.MakeSound() + "<br/>";
            result += "Dog Sound: " + a2.MakeSound() + "<br/>";

            lblResult.Text = result;
        }
    }

    // ---------- Method Overloading Example ----------
    public class Calculator
    {
        // Method 1
        public int Add(int a, int b)
        {
            return a + b;
        }

        // Method 2 (Overloaded)
        public double Add(double a, double b)
        {
            return a + b;
        }

        // Method 3 (Overloaded)
        public int Add(int a, int b, int c)
        {
            return a + b + c;
        }
    }

    // ---------- Method Overriding Example ----------
    public class Animal
    {
        public virtual string MakeSound()
        {
            return "Some generic animal sound";
        }
    }

    public class Dog : Animal
    {
        public override string MakeSound()
        {
            return "Bark! Bark!";
        }
    }
}

Explanation

1. Method Overloading

  • All three Add() methods belong to the same class (Calculator).

  • They differ by:

    • Number of parameters

    • Data types of parameters

  • The compiler decides which method to call based on parameter types — this is Compile-time Polymorphism.

Example

calc.Add(10, 20);       // Calls Add(int, int)
calc.Add(10.5, 20.5);   // Calls Add(double, double)
calc.Add(10, 20, 30);   // Calls Add(int, int, int)

2. Method Overriding

  • The Dog class inherits from the Animal class.

  • The method MakeSound() is redefined (overridden) using the override keyword.

  • The virtual keyword in the base class allows overriding.

  • The runtime type of the object determines which method runs — this is Run-time Polymorphism.

Example

Animal a1 = new Animal();  // Calls Animal.MakeSound()
Animal a2 = new Dog();     // Calls Dog.MakeSound()

Output on Web Page

Method Overloading:
Add(10, 20) = 30
Add(10.5, 20.5) = 31
Add(10, 20, 30) = 60

Method Overriding:
Animal Sound: Some generic animal sound
Dog Sound: Bark! Bark!

Real-Time Use Case

ConceptReal-Life Example
OverloadingIn a payment system, Pay() method can accept Card, UPI, or NetBanking parameters.
OverridingIn a logistics system, CalculateCost() is overridden by different transport modes like Air, Ship, Truck.

Conclusion

  • Method Overloading allows defining multiple methods with the same name but different parameters — handled at compile time.

  • Method Overriding allows redefining a method in a derived class — handled at runtime.

  • Together, they form the backbone of Polymorphism in Object-Oriented Programming (OOP).