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

Common Access ModifiersCommon Access Modifiers

You might already be familiar with the keyword public. It is an access modifier which defines where a certain class, method or a field can be accessed from.

Similarly there are terms like private and protected. Both of these terms can be used to specify the accessibility of a method or a field.

When we write public before a class, it simply makes the class accessible from different parts (files) of the program. We don't need to explore that aspect in detail as it is out of the scope of this chapter.

When we specify a method or a field as public, we basically make that property of the class accessible from anywhere inside the code through an object:

cs

index.cs

If we don't want a field or a method of an object to be accessible outside the class, we can simply make it private:

cs

index.cs

It is important to note that the private fields and methods are not inherited by the derived classes:

cs

index.cs

To make a field or method inaccessible from outside the class but accessible from child classes, we use the protected keyword:

cs

index.cs

Sometimes, to be able to access private attributes from a class we create public methods called getters. The method getMagnitude is an example of a getter. This ensures that the field is only readable and not writable, making that attribute read-only.

Note: If no access modifier is specified to a class member, it is assumed to be private by default.

1. Which access modifier allows a member to be accessible only within its defining class?
2. How to can make a class attribute inaccessible from outside?
3. What is a good way of accessing the values of private fields from outside the class?

Which access modifier allows a member to be accessible only within its defining class?

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

question-icon

How to can make a class attribute inaccessible from outside?

Виберіть кілька правильних відповідей

What is a good way of accessing the values of private fields from outside the class?

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

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

Секція 4. Розділ 3
course content

Зміст курсу

C# Beyond Basics

Common Access ModifiersCommon Access Modifiers

You might already be familiar with the keyword public. It is an access modifier which defines where a certain class, method or a field can be accessed from.

Similarly there are terms like private and protected. Both of these terms can be used to specify the accessibility of a method or a field.

When we write public before a class, it simply makes the class accessible from different parts (files) of the program. We don't need to explore that aspect in detail as it is out of the scope of this chapter.

When we specify a method or a field as public, we basically make that property of the class accessible from anywhere inside the code through an object:

cs

index.cs

If we don't want a field or a method of an object to be accessible outside the class, we can simply make it private:

cs

index.cs

It is important to note that the private fields and methods are not inherited by the derived classes:

cs

index.cs

To make a field or method inaccessible from outside the class but accessible from child classes, we use the protected keyword:

cs

index.cs

Sometimes, to be able to access private attributes from a class we create public methods called getters. The method getMagnitude is an example of a getter. This ensures that the field is only readable and not writable, making that attribute read-only.

Note: If no access modifier is specified to a class member, it is assumed to be private by default.

1. Which access modifier allows a member to be accessible only within its defining class?
2. How to can make a class attribute inaccessible from outside?
3. What is a good way of accessing the values of private fields from outside the class?

Which access modifier allows a member to be accessible only within its defining class?

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

question-icon

How to can make a class attribute inaccessible from outside?

Виберіть кілька правильних відповідей

What is a good way of accessing the values of private fields from outside the class?

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

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

Секція 4. Розділ 3
some-alt