OOP C# Programming Basics

Object-Oriented Programming: The Four Pillars Explained

June 03, 2026

Introduction

Object-Oriented Programming (OOP) is a programming paradigm that organises software design around data, or objects, rather than functions and logic. Understanding its four pillars is essential for writing clean, maintainable code.

1. Encapsulation

Encapsulation means bundling data (fields) and methods that operate on that data into a single unit (class), and restricting direct access from outside.

class BankAccount {
    private decimal _balance;

    public void Deposit(decimal amount) {
        if (amount > 0) _balance += amount;
    }

    public decimal GetBalance() => _balance;
}

The _balance field is hidden. External code can only interact through the public methods, protecting the integrity of the data.

2. Abstraction

Abstraction hides complex implementation details and exposes only what is necessary. Abstract classes and interfaces are the tools of abstraction.

abstract class Shape {
    public abstract double Area();
}

class Circle : Shape {
    private double _radius;
    public Circle(double r) { _radius = r; }
    public override double Area() => Math.PI * _radius * _radius;
}

3. Inheritance

Inheritance allows a class to derive properties and behaviour from another class, promoting code reuse.

class Animal {
    public string Name { get; set; }
    public virtual void Speak() => Console.WriteLine("...");
}

class Dog : Animal {
    public override void Speak() => Console.WriteLine($"{Name} says Woof!");
}

4. Polymorphism

Polymorphism allows objects of different types to be treated as objects of a common base type, while each behaves in its own way.

List<Shape> shapes = new() { new Circle(5), new Rectangle(4, 6) };
foreach (var s in shapes)
    Console.WriteLine(s.Area()); // Each calls its own Area()

Conclusion

These four pillars work together to make code modular, reusable, and easier to maintain. Mastering them is the foundation of professional software development.