Abstraction is one of the key principles of Object-Oriented Programming (OOP). It is the process of hiding implementation details and exposing only the necessary functionalities. This helps in reducing complexity and improving code maintainability.
Abstraction in C# can be achieved using abstract classes and interfaces.
// Abstract class abstract class BankAccount { protected double balance; protected string owner; public BankAccount(string owner, double initialBalance) { this.owner = owner; balance = initialBalance > 0 ? initialBalance : 0; } public abstract void Deposit(double amount); public abstract double GetBalance(); } // Derived class implementing the abstract methods class SavingsAccount : BankAccount { public SavingsAccount(string owner, double initialBalance) : base(owner, initialBalance) { } public override void Deposit(double amount) { if (amount > 0) { balance += amount; Console.WriteLine($"Deposited: {amount}"); } } public override double GetBalance() { return balance; } } var account = new SavingsAccount("John Doe", 1000); account.Deposit(500); Console.WriteLine($"Current Balance: {account.GetBalance()}");
BankAccount
Deposit
GetBalance
SavingsAccount
Abstraction simplifies code by hiding unnecessary details while exposing only essential functionalities. It enhances code flexibility, readability, and maintainability.
In the next articles, we will explore other Object-Oriented Programming principles such as Inheritance and Interfaces using a consistent example structure.
I am Ready to Make Your Project Process Happy.
For Your Great Business Idea; Together, We Can Create a Perfect Solution.
I Have Rich Experience and Knowledge in Our Industry.
This website uses cookies to improve your experience.