Lesson 1
Encapsulation in C#: Safeguarding Data in OOP
Introduction

Welcome! Today's subject is Encapsulation, a cornerstone of Object-Oriented Programming (OOP). Encapsulation bundles data and the operations that we perform on them into one unit, namely an object. It guards data against unwanted alterations, ensuring the creation of robust and maintainable software.

Prepare yourself for an exciting journey as we delve into how encapsulation works and explore the vital role it plays in data privacy.

Unraveling Encapsulation

Starting with the basics, encapsulation is similar to packing data and the methods that modify this data into a single compartment known as a class. It safeguards the data in an object from external interference.

To illustrate, consider a C# class representing a bank account. Without encapsulation, the account balance could be directly altered. With encapsulation, however, the balance can only change through specified methods, like depositing or withdrawing.

C#
1public class BankAccount 2{ 3 public double Balance; // no encapsulation 4 5 // Method to withdraw 6 public void Withdraw(double amount) 7 { 8 this.Balance -= amount; 9 } 10 11 // Method to deposit 12 public void Deposit(double amount) 13 { 14 this.Balance += amount; 15 } 16} 17 18public class Program 19{ 20 public static void Main(string[] args) 21 { 22 BankAccount account = new BankAccount(); 23 account.Balance += 1000; // directly accessing the balance 24 } 25}
Encapsulation: Guardian of Data Privacy

Encapsulation restricts direct access to an object's data and prevents unwanted data alteration. This principle is comparable to window blinds, allowing you to look out while preventing others from peeping in.

In encapsulation, private and public attributes are integral to data privacy. Private attributes, indicated by the private keyword, require caution while being manipulated.

To illustrate, let's consider a C# class named Person, which includes a private attribute Name.

C#
1public class Person 2{ 3 // Private attribute 4 private string name; 5 6 // Constructor 7 public Person(string name) 8 { 9 this.name = name; 10 } 11 12 // Accessor method 13 public string GetName() 14 { 15 return this.name; 16 } 17 18 public static void Main(string[] args) 19 { 20 Person person = new Person("Alice"); 21 System.Console.WriteLine(person.GetName()); // Accessing private attribute via accessor method. Output: Alice 22 // The following line would cause an error due to private access: 23 // System.Console.WriteLine(person.name); 24 } 25}

In this example, name is private, and GetName() enables us to access name. However, we don't provide a method to change the name, preventing alterations.

To designate an attribute as private, we use the private keyword.

Getter and Setter Methods in Encapsulation

Within encapsulation, C# uses getter and setter methods to access or modify private attributes. In a class, the getter method retrieves the attribute value, and the setter method alters it. Let's illustrate this.

C#
1public class Dog 2{ 3 // Private attribute 4 private string name; 5 6 // Constructor 7 public Dog(string name) 8 { 9 this.name = name; 10 } 11 12 // Setter method 13 public void SetName(string name) 14 { 15 this.name = name; 16 } 17 18 // Getter method 19 public string GetName() 20 { 21 return this.name; 22 } 23 24 public static void Main(string[] args) 25 { 26 Dog myDog = new Dog("Max"); 27 myDog.SetName("Buddy"); 28 System.Console.WriteLine(myDog.GetName()); // Output: Buddy 29 } 30}

Here, SetName() and GetName() serve as the setter and getter methods, respectively, for the private attribute name.

Practical Application and Summary

Let's apply the principle of encapsulation to our BankAccount class, which includes private attributes like an account number and balance, along with public methods for withdrawals, deposits, and balance checks.

C#
1public class BankAccount 2{ 3 // Private attributes 4 private int accountNo; 5 private double balance; 6 7 // Constructor 8 public BankAccount(int accountNo, double balance) 9 { 10 this.accountNo = accountNo; 11 this.balance = balance; 12 } 13 14 // Method to withdraw money 15 public void Withdraw(double amount) 16 { 17 if (amount > 0 && amount <= balance) 18 { 19 this.balance -= amount; 20 } 21 else 22 { 23 System.Console.WriteLine("Invalid amount or insufficient balance."); 24 } 25 } 26 27 // Method to deposit money 28 public void Deposit(double amount) 29 { 30 if (amount > 0) 31 { 32 this.balance += amount; 33 } 34 else 35 { 36 System.Console.WriteLine("Invalid deposit amount."); 37 } 38 } 39 40 // Method to check balance 41 public double CheckBalance() 42 { 43 return this.balance; 44 } 45 46 public static void Main(string[] args) 47 { 48 BankAccount account = new BankAccount(1, 500.0); 49 account.Withdraw(100); 50 account.Deposit(50); 51 System.Console.WriteLine(account.CheckBalance()); // Prints: 450.0 52 } 53}

In the above code, the BankAccount class encapsulates account details that we would like to be hidden from the outer scope, and the public methods manipulate the balance in a controlled way. This way, we limit the potential interaction with the account's balance and other private information, improving security.

Hands-on Exercise and Practice

Admirable! Now it's your turn to apply what you've learned by practicing encapsulation in C#. Remember, practice enhances your comprehension. Enjoy coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.