OOP Principles โ Encapsulation, Inheritance, Polymorphism, Abstraction
Master the four pillars of Object-Oriented Programming โ Encapsulation, Inheritance, Polymorphism, and Abstraction โ the language every software architect speaks. C# live examples throughout.
โBefore we can talk about design patterns, SOLID principles, and architectural styles, we need to understand the programming paradigm they all build upon: Object-Oriented Programming. OOP organizes software around objects โ entities that bundle data and behavior together. In OOP, the data and the functions that operate on it live together in a class. A BankAccount has a balance โ that's its data. It has Deposit(), Withdraw(), and GetBalance() methods โ that's its behavior. OOP gives us four fundamental pillars: Encapsulation, Inheritance, Polymorphism, and Abstraction. These are the vocabulary of software architecture โ every design pattern and SOLID principle builds directly on these ideas. This course uses C# and .NET throughout.โ
โEncapsulation is the first pillar of OOP: bundling data and the methods that operate on it, while hiding internals from the outside world. In C#, fields are declared private with an underscore prefix by convention. Access is controlled through properties and methods. The BankAccount class: _balance is private โ no external code can write account._balance = -1000. Instead, Deposit() validates the amount before adding it. The Balance property has a private setter โ readable from outside but writable only internally. This protects data integrity and lets you change implementation without breaking callers.โ
โInheritance allows a class to acquire the properties and behaviors of another class. In C#, you use a colon: class Dog : Animal. Dog inherits everything from Animal โ the Name property, the Eat() method, the Sleep() method โ without rewriting any of it. Dog can add its own properties like Breed and override virtual methods. The Animal base class marks MakeSound() as virtual. Dog overrides it and returns 'Woof', Cat returns 'Meow'. The relationship is is-a: Dog IS-A Animal. Wherever code expects an Animal, you can pass a Dog and it works correctly.โ
โPolymorphism means 'many forms'. In C#: Animal[] animals = { new Dog(), new Cat(), new Lion() }. Loop: foreach (var animal in animals) animal.MakeSound(). Each call goes to the actual type's override โ Dog prints Woof, Cat prints Meow, Lion prints Roar. The loop is completely unaware of concrete types. This eliminates brittle type-switching code. When you add a new Elephant class with an override, the existing loop handles it automatically. This is the Open/Closed Principle at the OOP level.โ
โAbstraction means exposing only what is necessary and hiding the complexity behind a clean contract. In C#, abstraction is achieved through abstract classes and interfaces. An abstract class cannot be instantiated. The Shape abstract class declares public abstract double Area() with no body โ every subclass must provide the formula. Circle overrides Area() as Math.PI * r * r. Rectangle returns width * height. The calling code holds Shape references and calls Area() โ it has no idea what shape it's dealing with or how the area is computed. Interfaces go further โ they're pure contracts with no implementation at all. Depending on interfaces rather than concrete classes is the foundation of dependency injection and testable .NET code.โ
โThe four pillars work together in every well-designed C# system. Encapsulation keeps private fields private and enforces invariants through properties and methods. Inheritance structures your class hierarchy โ but use it only for genuine is-a relationships. Polymorphism makes the hierarchy useful at runtime through virtual/override dispatch. Abstraction via abstract classes and interfaces defines the contracts that make dependency injection and unit testing possible. In the next episode we explore SOLID โ the five rules that prevent OOP systems from deteriorating into unmaintainable spaghetti.โ
- 1Build a BankAccount class demonstrating Encapsulation with private fields and public getters/setters
- 2Create an Animal class hierarchy using Inheritance โ Dog, Cat extending Animal
- 3Demonstrate Polymorphism with method overriding โ same method call, different behavior
- 4Design an abstract Shape class with abstract area() method implemented by Circle and Rectangle