Skip to content

Object-Oriented Programming

Object-oriented programming (OOP) is a programming paradigm that organizes code into objects, which are instances of classes. It is a way of designing and structuring software to model real-world entities and their relationships. OOP is based on the following core principles:

  • Objects: Objects are instances of classes and represent real-world entities with attributes (properties) and behaviors (methods). For example, if you are modeling a "Car" in a program, the object would have properties like color, model, and speed, and behaviors like accelerating, braking, and turning.

  • Classes: A class is a blueprint or a template that defines the properties and behaviors common to all objects of a certain type. It serves as a blueprint for creating objects. In the car example, the class would define what properties and methods all cars should have.

  • Encapsulation: Encapsulation is the bundling of data (properties) and methods that operate on the data into a single unit known as a class. It hides the internal details of the object and allows access only through well-defined interfaces.

  • Inheritance: Inheritance is a mechanism that allows a class (subclass or derived class) to inherit the properties and behaviors of another class (base class or superclass). It promotes code reuse and the creation of a hierarchy of classes.

  • Polymorphism: Polymorphism allows objects of different types to be treated as objects of a common base type. It enables a single interface to represent different types of objects. Polymorphism can be achieved through method overloading and method overriding.

OOP aims to model the real-world in a way that is intuitive and easy to understand, making it easier to design, develop, and maintain complex software systems. By using objects, classes, and their relationships, OOP provides a modular and organized approach to software development, facilitating code reuse, extensibility, and maintenance.

Future Lesson

OOP Inheritance and Polymorphism will be covered in a future module.

Encapsulation

Encapsulation is one of the core principles of object-oriented programming (OOP) and involves bundling the data (attributes or properties) and methods (functions or behavior) that operate on the data into a single unit known as a class. It also includes the concept of access modifiers, which control the visibility of class members.

Encapsulation provides several benefits, including data hiding, improved maintainability, and the ability to control access to the internal state of an object.

Key Concepts of Encapsulation:

  1. Controlled access to the internal state (data) of an object.
  2. Methods define the behavior and operations that can be performed on the object's data.

Data Hiding

Data hiding, also known as information hiding, is a concept in computer science and software engineering that refers to the practice of restricting access to certain details of an object and only exposing what is necessary for the external world to interact with that object. The primary goal of data hiding is to protect the internal implementation details of an object and provide a well-defined interface for interacting with the object.

Access Modifiers

Access modifiers, such as private, public, and protected, control the visibility of class members. Private members are not directly accessible from outside the class, providing a level of data hiding.

The following is a class that demonstrates encapsulation:

C#
public class BankAccount
{
    // Private fields (data hiding)
    private string accountHolder;
    private double balance;

    // Method to get account holder
    public string GetAccountHolder()
    {
        return accountHolder;
    }

    // Method to set account holder with validation
    public void SetAccountHolder(string holder)
    {
        accountHolder = holder;
    }

    // Method to get balance
    public double GetBalance()
    {
        return balance;
    }

    // Method to deposit money
    public void Deposit(double amount)
    {
        if (amount > 0)
        {
            balance += amount;
        }
    }

    // Method to withdraw money
    public void Withdraw(double amount)
    {
        if (amount > 0 && amount <= balance)
        {
            balance -= amount;
        }
    }
}

Here is a sample of how the BankAccount class could be used in a program.

C#
class Program
{
    static void Main()
    {
        // Create an object of the BankAccount class
        BankAccount account = new BankAccount();

        // Set account holder name using the method
        account.SetAccountHolder("John Doe");

        // Attempting to set an empty account holder name (validation in method)
        account.SetAccountHolder("");

        // Display account details using methods
        Console.WriteLine($"Account Holder: {account.GetAccountHolder()}");
        Console.WriteLine($"Balance: {account.GetBalance():C}");
    }
}

In this example, each private field (accountHolder and balance) is accessed and modified through separate getter and setter methods. The following code would not be legal syntax:

C#
// Display account details using methods
Console.WriteLine($"Account Holder: {account.accountHolder)}");
Console.WriteLine($"Balance: {account.balance:C}");