0% found this document useful (0 votes)
10 views5 pages

Understanding Polymorphism in C#

Polymorphism refers to the ability of a single function or method to operate in different ways based on the input parameters or the object type. The document provides examples of method overloading in a banking context, showcasing different deposit methods, and method overriding with a payment system demonstrating different payment methods. Both concepts illustrate how polymorphism enhances code flexibility and reusability in programming.

Uploaded by

Niranjan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Understanding Polymorphism in C#

Polymorphism refers to the ability of a single function or method to operate in different ways based on the input parameters or the object type. The document provides examples of method overloading in a banking context, showcasing different deposit methods, and method overriding with a payment system demonstrating different payment methods. Both concepts illustrate how polymorphism enhances code flexibility and reusability in programming.

Uploaded by

Niranjan
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

What is Polymorphism?

Polymorphism means “one name, many forms”.

Method Overloading – Real-Time Banking Example (C#)


In a banking system, deposit can happen in different ways:
 Deposit only amount
 Deposit amount + account type
 Deposit amount + account type + branch
➡️Same operation (Deposit) but different inputs
➡️Perfect use case for Method Overloading
Program:
using System;
class BankAccount
{
string accountHolder;
double balance;
public BankAccount(string name, double initialBalance)
{
accountHolder = name;
balance = initialBalance;
}
// Deposit using amount only
public void Deposit(double amount)
{
balance += amount;
[Link]($"Deposited ₹{amount}");
[Link]($"Balance: ₹{balance}");
}
// Deposit using amount and account type
public void Deposit(double amount, string accountType)
{
balance += amount;
[Link]($"Deposited ₹{amount} into {accountType} account");
[Link]($"Balance: ₹{balance}");
}
// Deposit using amount, account type, and branch
public void Deposit(double amount, string accountType, string branch)
{
balance += amount;
[Link]($"Deposited ₹{amount} into {accountType} account at
{branch} branch");
[Link]($"Balance: ₹{balance}");
}
}
class Program
{
static void Main()
{
BankAccount acc = new BankAccount("Ravi", 10000);
[Link](2000);
[Link]();
[Link](3000, "Savings");
[Link]();
[Link](5000, "Savings", "Hyderabad");
[Link]();
}
}
Program With Return Type:
using System;
class BankAccount
{
string accountHolder;
double balance;
public BankAccount(string name, double initialBalance)
{
accountHolder = name;
balance = initialBalance;
}
// Deposit using amount only
public void Deposit(double amount)
{
balance += amount;
[Link]($"Deposited ₹{amount}");
[Link]($"Balance: ₹{balance}");
}
// Deposit using amount and account type
public void Deposit(double amount, string accountType)
{
balance += amount;
[Link]($"Deposited ₹{amount} into {accountType} account");
[Link]($"Balance: ₹{balance}");
}
// Deposit using amount, account type, and branch
public void Deposit(double amount, string accountType, string branch)
{
balance += amount;
[Link]($"Deposited ₹{amount} into {accountType} account at
{branch} branch");
[Link]($"Balance: ₹{balance}");
}
}
class Program
{
static void Main()
{
BankAccount acc = new BankAccount("Ravi", 10000);
[Link](2000);
[Link]();
[Link](3000, "Savings");
[Link]();
[Link](5000, "Savings", "Hyderabad");
[Link]();
}
}

Method Overriding (runtime polymorphism):


using System;
class Employee
{
public virtual double CalculateSalary()
{
return 0;
}
}
class Manager : Employee
{
public override double CalculateSalary()
{
return 50000 + 10000; // Basic + Bonus
}
}
class Developer : Employee
{
public override double CalculateSalary()
{
return 40000 + 8000; // Basic + Incentive
}
}
class Tester : Employee
{
public override double CalculateSalary()
{
return 30000 + 5000;
}
}
class Program
{
static void Main()
{
Employee e;
e = new Manager();
[Link]("Manager Salary: " + [Link]());
e = new Developer();
[Link]("Developer Salary: " + [Link]());
e = new Tester();
[Link]("Tester Salary: " + [Link]());
}
}

using System;
class Payment
{
public virtual void Pay()
{
[Link]("Processing payment...");
}
}
class CreditCard : Payment
{
public override void Pay()
{
[Link]("Payment done using Credit Card");
}
}
class UPI : Payment
{
public override void Pay()
{
[Link]("Payment done using UPI");
}
}
class Cash : Payment
{
public override void Pay()
{
[Link]("Payment done using Cash");
}
}
class Program
{
static void Main()
{
Payment p;
p = new CreditCard();
[Link]();
p = new UPI();
[Link]();
p = new Cash();
[Link]();
}
}

Real-Time Example 2: Payment Gateway System


using System;
class Payment
{
public virtual void Pay()
{
[Link]("Processing payment...");
}
}
class CreditCard : Payment
{
public override void Pay()
{
[Link]("Payment done using Credit Card");
}
}
class UPI : Payment
{
public override void Pay()
{
[Link]("Payment done using UPI");
}
}
class Cash : Payment
{
public override void Pay()
{
[Link]("Payment done using Cash");
}
}
class Program
{
static void Main()
{
Payment p;
p = new CreditCard();
[Link]();
p = new UPI();
[Link]();
p = new Cash();
[Link]();
}
}

You might also like