Give an example of abstraction in C#.?
Real-World Example: Payment Processing
// Abstract class
abstract class Payment
public abstract void Pay(decimal amount);
public void ShowReceipt(decimal amount) =>
Console.WriteLine($"Paid: {amount:C}");
// Derived classes implement abstraction
class CreditCardPayment : Payment
public override void Pay(decimal amount) =>
Console.WriteLine($"Paid {amount:C} using Credit Card");
class PayPalPayment : Payment
public override void Pay(decimal amount) =>
Console.WriteLine($"Paid {amount:C} using PayPal");
// Usage
Payment payment1 = new CreditCardPayment();
payment1.Pay(500);
payment1.ShowReceipt(500);
Payment payment2 = new PayPalPayment();
payment2.Pay(300);
payment2.ShowReceipt(300);
Explanation:
- Payment defines what a payment should do (abstract method Pay).
- Derived classes (CreditCardPayment, PayPalPayment) define how payment is
made.
- Users interact only with the abstract interface, not the internal logic.
🔹 Section 4: Inheritance – Interview Q&A