Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Encapsulation | OOP Principles
C# Beyond Basics

EncapsulationEncapsulation

Encapsulation is essentially just a method of data organization. It involves organizing data and methods in the forms of classes so the whole program is essentially just based around classes and the data and methods are encapsulated within those classes. This encapsulation provides a way to control access to the internal state of an object, promoting modularity, maintainability, and security in a software system.

In Encapsulation, we use the Access Modifiers like public, private and protected to hide all most of the fields and methods of a class and expose only the ones that are needed to be used from outside.

Since most of the data is directly inaccessible outside the class, we use getters and setters to access or modify the data.

One good example is a Customer class which defines the customer of a bank:

cs

index.cs

In the above example, no field is directly accessible or modifiable from outside. Instead we use methods like Deposit and Withdraw to modify the value whenever needed. Similarly to access the value of the balance we use the getBalance method.

The public keyword is generally discouraged to be used unless necessary.

To remind, in case we want to keep a field inaccessible from outside the class but accessible from the derived classes, we can use the `protected` access modifier.
1. What role do access modifiers play in encapsulation in C#?
2. Which of the following format specifiers should to used to a **minimal** amount to ensure Encapsulation?

What role do access modifiers play in encapsulation in C#?

Виберіть правильну відповідь

Which of the following format specifiers should to used to a minimal amount to ensure Encapsulation?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 5. Розділ 6
course content

Зміст курсу

C# Beyond Basics

EncapsulationEncapsulation

Encapsulation is essentially just a method of data organization. It involves organizing data and methods in the forms of classes so the whole program is essentially just based around classes and the data and methods are encapsulated within those classes. This encapsulation provides a way to control access to the internal state of an object, promoting modularity, maintainability, and security in a software system.

In Encapsulation, we use the Access Modifiers like public, private and protected to hide all most of the fields and methods of a class and expose only the ones that are needed to be used from outside.

Since most of the data is directly inaccessible outside the class, we use getters and setters to access or modify the data.

One good example is a Customer class which defines the customer of a bank:

cs

index.cs

In the above example, no field is directly accessible or modifiable from outside. Instead we use methods like Deposit and Withdraw to modify the value whenever needed. Similarly to access the value of the balance we use the getBalance method.

The public keyword is generally discouraged to be used unless necessary.

To remind, in case we want to keep a field inaccessible from outside the class but accessible from the derived classes, we can use the `protected` access modifier.
1. What role do access modifiers play in encapsulation in C#?
2. Which of the following format specifiers should to used to a **minimal** amount to ensure Encapsulation?

What role do access modifiers play in encapsulation in C#?

Виберіть правильну відповідь

Which of the following format specifiers should to used to a minimal amount to ensure Encapsulation?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 5. Розділ 6
some-alt