Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Encapsulation in Practice | Class Design and Encapsulation
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C# OOP Class Construction Drills

bookEncapsulation in Practice

Encapsulation is a fundamental concept in object-oriented programming that helps you design classes with robust, controlled access to their internal data. It means hiding the internal state of an object and requiring all interaction to occur through well-defined methods. This practice is essential for robust class design because it protects the integrity of your data, makes your code easier to maintain, and reduces the chance of bugs caused by unintended modifications.

Note
Definition

Encapsulation is the practice of keeping the internal details of a class hidden from the outside world, exposing only what is necessary through public methods. In object-oriented programming, encapsulation ensures that objects control their own data and behavior, leading to safer and more maintainable code.

Program.cs

Program.cs

copy
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
using System; namespace ConsoleApp { public class BankAccount { private decimal balance; public BankAccount(decimal initialBalance) { balance = initialBalance; } public void Deposit(decimal amount) { if (amount > 0) { balance += amount; } } public bool Withdraw(decimal amount) { if (amount > 0 && amount <= balance) { balance -= amount; return true; } return false; } public decimal GetBalance() { return balance; } } public class Program { public static void Main(string[] args) { BankAccount account = new BankAccount(100); account.Deposit(50); bool success = account.Withdraw(30); Console.WriteLine("Balance: " + account.GetBalance()); Console.WriteLine("Withdrawal successful: " + success); } } }

In the BankAccount class, encapsulation is achieved by declaring the balance field as private. This means that code outside the class cannot access or modify balance directly. Instead, you must use the public methods Deposit, Withdraw, and GetBalance to interact with the balance.

By restricting direct access, you ensure that only valid operations—like adding a positive amount or withdrawing only if there are sufficient funds—can change the state of the account. This prevents accidental or malicious changes that could leave the object in an invalid state.

PublicFieldsExample.cs

PublicFieldsExample.cs

copy
123456
// This code is for illustration only and should not be used in practice. public class BankAccount { public decimal balance; }

If you make fields like balance public, as shown above, any code can change the value to anything at any time. This can lead to serious problems, such as setting the balance to a negative number, bypassing business rules, or causing bugs that are hard to trace. Encapsulation helps you avoid these risks by forcing all changes to go through controlled methods.

1. What is encapsulation?

2. Why should fields usually be private in a class?

3. How does encapsulation help prevent bugs?

question mark

What is encapsulation?

Select the correct answer

question mark

Why should fields usually be private in a class?

Select the correct answer

question mark

How does encapsulation help prevent bugs?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 3

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Suggested prompts:

Can you give an example of encapsulation in code?

Why is encapsulation important in real-world applications?

How does encapsulation differ from other OOP principles?

bookEncapsulation in Practice

Veeg om het menu te tonen

Encapsulation is a fundamental concept in object-oriented programming that helps you design classes with robust, controlled access to their internal data. It means hiding the internal state of an object and requiring all interaction to occur through well-defined methods. This practice is essential for robust class design because it protects the integrity of your data, makes your code easier to maintain, and reduces the chance of bugs caused by unintended modifications.

Note
Definition

Encapsulation is the practice of keeping the internal details of a class hidden from the outside world, exposing only what is necessary through public methods. In object-oriented programming, encapsulation ensures that objects control their own data and behavior, leading to safer and more maintainable code.

Program.cs

Program.cs

copy
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
using System; namespace ConsoleApp { public class BankAccount { private decimal balance; public BankAccount(decimal initialBalance) { balance = initialBalance; } public void Deposit(decimal amount) { if (amount > 0) { balance += amount; } } public bool Withdraw(decimal amount) { if (amount > 0 && amount <= balance) { balance -= amount; return true; } return false; } public decimal GetBalance() { return balance; } } public class Program { public static void Main(string[] args) { BankAccount account = new BankAccount(100); account.Deposit(50); bool success = account.Withdraw(30); Console.WriteLine("Balance: " + account.GetBalance()); Console.WriteLine("Withdrawal successful: " + success); } } }

In the BankAccount class, encapsulation is achieved by declaring the balance field as private. This means that code outside the class cannot access or modify balance directly. Instead, you must use the public methods Deposit, Withdraw, and GetBalance to interact with the balance.

By restricting direct access, you ensure that only valid operations—like adding a positive amount or withdrawing only if there are sufficient funds—can change the state of the account. This prevents accidental or malicious changes that could leave the object in an invalid state.

PublicFieldsExample.cs

PublicFieldsExample.cs

copy
123456
// This code is for illustration only and should not be used in practice. public class BankAccount { public decimal balance; }

If you make fields like balance public, as shown above, any code can change the value to anything at any time. This can lead to serious problems, such as setting the balance to a negative number, bypassing business rules, or causing bugs that are hard to trace. Encapsulation helps you avoid these risks by forcing all changes to go through controlled methods.

1. What is encapsulation?

2. Why should fields usually be private in a class?

3. How does encapsulation help prevent bugs?

question mark

What is encapsulation?

Select the correct answer

question mark

Why should fields usually be private in a class?

Select the correct answer

question mark

How does encapsulation help prevent bugs?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 2. Hoofdstuk 3
some-alt