The four pillars of object-oriented programming are encapsulation, abstraction, inheritance and polymorphism. Together they help organise code into objects that are easier to understand, reuse and change. 1. Encapsulation Bundling data and the methods that operate on it inside a class, and restricting direct access to the internal state. public class BankAccount { private decimal _balance; // hidden state public decimal Balance => _balance; // read-only access public void Deposit(decimal amount) { if (amount <= 0) throw new ArgumentException("Deposit must be positive."); _balance += amount; } public void Withdraw(decimal amount) { if (amount > _balance) throw new InvalidOperationException("Insufficient funds."); _balance -= amount; } } Benefit: the object protects its own rules (no negative deposits, no overdraft), and callers cannot put it into an invalid state. 2. Abstraction Exposing only the essential features and hiding implementation details. Callers know what an object does, not how. public interface INotificationService { Task SendAsync(string recipient, string message); } The caller sends a notification without knowing whether it uses email, SMS, or a push service. Abstract classes and interfaces are the main tools. Encapsulation vs abstraction: encapsulation hides data and protects state (implementation level); abstraction hides complexity and defines simple interfaces (design level). 3. Inheritance A class (derived or child) acquires the properties and methods of another class (base or parent), enabling reuse and an "is-a" relationship. public class Employee { public string Name { get; init; } = ""; public virtual decimal CalculatePay() => 30_000m; } public class Manager : Employee { public decimal Bonus { get; init; } public override decimal CalculatePay() => base.CalculatePay() + Bonus; } Benefit: shared behaviour lives in one place. Caution: deep inheritance hierarchies become rigid; prefer composition when there is no true "is-a" relationship. C# and Java support single class inheritance plus multiple interfaces. 4. Polymorphism "Many forms": the same call behaves differently depending on the object's actual type. - Runtime polymorphism: method overriding through base classes or interfaces. List<Employee> staff = [new Employee { Name = "Asha" }, new Manager { Name = "Ravi", Bonus = 10_000 }]; foreach (var e in staff) Console.WriteLine($"{e.Name}: {e.CalculatePay()}"); // each type calculates its own pay - Compile-time polymorphism: method overloading (same name, different parameters) and generics. How they work together in a real design A payment system defines an IPaymentGateway abstraction; each gateway class encapsulates its own API details; a base class may share common logging or retry behaviour through inheritance; and the checkout code uses polymorphism to call ChargeAsync on whichever gateway is configured. Interview tip: define each pillar in one sentence, give a short code example, and mention a benefit and a pitfall (for example, overusing inheritance).
Ravindra Wadile · · 0 likes