Abstraction means hiding the implementation details nobody needs and only exposing the features the user actually cares about. In C#, you can use abstract classes and abstract methods to set up a common structure.
csharp
abstract class PaymentMethod
{
public abstract void Pay(decimal amount);
public void PrintReceipt(decimal amount)
{
Console.WriteLine($"Receipt amount: {amount}");
}
}
class CardPayment : PaymentMethod
{
public override void Pay(decimal amount)
{
Console.WriteLine($"Paid by card: {amount}");
}
}
class Program
{
static void Main(string[] args)
{
PaymentMethod payment = new CardPayment();
payment.Pay(25.50m);
payment.PrintReceipt(25.50m);
}
}How it works
PaymentMethodis an abstract class, so you can't create an object from it directly.Paymethod must be overridden by every derived class — there's no way around it.PrintReceiptis a common method that every derived class can use.
You should see
Paid by card: 25.50 Receipt amount: 25.50