Object-oriented programming (OOP) is a programming paradigm centered around "objects" that contain both data (attributes) and code (methods) to manipulate that data. It's a fundamental concept in modern software development, especially in languages like C#. Understanding OOP principles is crucial for building scalable, maintainable, and robust applications. This lesson will introduce you to the core concepts of OOP, laying the groundwork for more advanced topics in C#.

Core Principles of Object-Oriented Programming

OOP is built upon four fundamental principles: Encapsulation, Abstraction, Inheritance, and Polymorphism. These principles work together to create a powerful and flexible programming paradigm.

Encapsulation

Encapsulation is the bundling of data (attributes) and methods that operate on that data into a single unit, or "class," and restricting direct access to some of the object's components. Think of it as a protective shield around the data, preventing unauthorized access and modification.

Example:

Consider a BankAccount class. The account balance should not be directly accessible or modifiable from outside the class. Encapsulation ensures that the balance can only be accessed and modified through specific methods like Deposit and Withdraw.

public class BankAccount
{
    private decimal balance; // Private attribute

    public BankAccount(decimal initialBalance)
    {
        balance = initialBalance;
    }

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

    public bool Withdraw(decimal amount)
    {
        if (amount > 0 && balance >= amount)
        {
            balance -= amount;
            return true;
        }
        return false;
    }

    public decimal GetBalance() // Public method to access balance
    {
        return balance;
    }
}

public class Example
{
    public static void Main(string[] args)
    {
        BankAccount account = new BankAccount(1000);
        // account.balance = -1000; // This would be illegal if balance was private
        account.Deposit(500);
        Console.WriteLine("Balance: " + account.GetBalance()); // Output: Balance: 1500
        account.Withdraw(200);
        Console.WriteLine("Balance: " + account.GetBalance()); // Output: Balance: 1300
    }
}

In this example, the balance attribute is private, meaning it can only be accessed from within the BankAccount class. The DepositWithdraw, and GetBalance methods provide controlled access to the balance.

Counterexample:

If the balance attribute were public, any part of the code could directly modify it, potentially leading to inconsistencies or errors. For instance, someone could set the balance to a negative value without using the Withdraw method, which might have additional logic to prevent overdrafts.

Abstraction

Abstraction is the process of simplifying complex reality by modeling classes based on essential characteristics, ignoring non-essential details. It focuses on what an object does rather than how it does it.

Example:

Consider a Car class. A user of the Car class doesn't need to know the intricate details of how the engine works, how the transmission shifts gears, or how the braking system operates. They only need to know how to start the car, accelerate, brake, and steer.

public class Car
{
    public void Start()
    {
        // Complex engine starting logic hidden here
        Console.WriteLine("Car started.");
    }

    public void Accelerate()
    {
        // Complex acceleration logic hidden here
        Console.WriteLine("Car accelerating.");
    }

    public void Brake()
    {
        // Complex braking logic hidden here
        Console.WriteLine("Car braking.");
    }

    public void Steer(string direction)
    {
        // Complex steering logic hidden here
        Console.WriteLine("Car steering " + direction);
    }
}

public class Example
{
    public static void Main(string[] args)
    {
        Car myCar = new Car();
        myCar.Start();
        myCar.Accelerate();
        myCar.Steer("left");
        myCar.Brake();
    }
}