Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
OOP Principles: Encapsulation | OOP
course content

Зміст курсу

Java OOP

OOP Principles: EncapsulationOOP Principles: Encapsulation

Encapsulation

Another fundamental principle of Object-Oriented Programming (OOP). Probably the most crucial principle as the entire OOP paradigm relies on it. You have already encountered this principle in this chapter when designating fields and methods as private or public. But now we will discuss this principle in more detail.

Encapsulation in Java is the practice of bundling data (attributes) and methods (functions) that operate on the data into a single unit called a class. This concept restricts direct access to the data from outside the class and enforces controlled access through methods, ensuring data integrity and enhancing security. In simpler terms, encapsulation hides the internal details of a class and provides a controlled interface for interacting with the class's data and behavior.

Note

Please do not confuse the word 'interface' (a template for creation, a pattern) with the Interface that we will be studying in the next section!

Access modifiers

Access modifiers are an integral part of encapsulation, so even though we have discussed them before, we need to review this material. Below is the definition of access modifiers along with a table and a brief description:

Access Modifiers in Java determine the accessibility of classes, methods, and variables in different parts of your code. There are four main access modifiers.
  1. public: Members with public access modifier are accessible from anywhere in the code. They have the widest scope. For example, a public method can be called from any class;
  2. private: Members with private access modifier are only accessible within the class where they are declared. They are not visible to external classes. This provides strong encapsulation;
  3. protected: Members with protected access modifier are accessible within the same package and by subclasses, even if they are in different packages. This allows controlled sharing of information;
  4. default (package-private): If no access modifier is specified, the member is accessible only within the same package. It's a default level of access.

Let's see how access modifiers work in practice. There are two classes: one created in the same package as the main class, and the other created in a different package. Let's see how access modifiers affect this:

Image 1
Image 2
Image 3

In the screenshots, you can see where variables with corresponding names are accessible from.

I will also remind you that if you want to bypass access modifiers, you can read about it here: link.

Note

It's also worth mentioning that methods can also have access modifiers. They work in the same way as with fields. For example, a method with a private access modifier will only be accessible in the class where it was created.

Summary

Encapsulation is very helpful when data needs to be organized in compartments, making the code highly structured. Additionally, thanks to encapsulation, you can configure where and how your fields and methods can be accessed.

1. What is encapsulation in Java?
2. Which access modifier is the strictest?
3. What are the benefits of encapsulation?
4. Which of the following statements about encapsulation are true?

What is encapsulation in Java?

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

Which access modifier is the strictest?

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

What are the benefits of encapsulation?

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

question-icon

Which of the following statements about encapsulation are true?

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

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

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

Зміст курсу

Java OOP

OOP Principles: EncapsulationOOP Principles: Encapsulation

Encapsulation

Another fundamental principle of Object-Oriented Programming (OOP). Probably the most crucial principle as the entire OOP paradigm relies on it. You have already encountered this principle in this chapter when designating fields and methods as private or public. But now we will discuss this principle in more detail.

Encapsulation in Java is the practice of bundling data (attributes) and methods (functions) that operate on the data into a single unit called a class. This concept restricts direct access to the data from outside the class and enforces controlled access through methods, ensuring data integrity and enhancing security. In simpler terms, encapsulation hides the internal details of a class and provides a controlled interface for interacting with the class's data and behavior.

Note

Please do not confuse the word 'interface' (a template for creation, a pattern) with the Interface that we will be studying in the next section!

Access modifiers

Access modifiers are an integral part of encapsulation, so even though we have discussed them before, we need to review this material. Below is the definition of access modifiers along with a table and a brief description:

Access Modifiers in Java determine the accessibility of classes, methods, and variables in different parts of your code. There are four main access modifiers.
  1. public: Members with public access modifier are accessible from anywhere in the code. They have the widest scope. For example, a public method can be called from any class;
  2. private: Members with private access modifier are only accessible within the class where they are declared. They are not visible to external classes. This provides strong encapsulation;
  3. protected: Members with protected access modifier are accessible within the same package and by subclasses, even if they are in different packages. This allows controlled sharing of information;
  4. default (package-private): If no access modifier is specified, the member is accessible only within the same package. It's a default level of access.

Let's see how access modifiers work in practice. There are two classes: one created in the same package as the main class, and the other created in a different package. Let's see how access modifiers affect this:

Image 1
Image 2
Image 3

In the screenshots, you can see where variables with corresponding names are accessible from.

I will also remind you that if you want to bypass access modifiers, you can read about it here: link.

Note

It's also worth mentioning that methods can also have access modifiers. They work in the same way as with fields. For example, a method with a private access modifier will only be accessible in the class where it was created.

Summary

Encapsulation is very helpful when data needs to be organized in compartments, making the code highly structured. Additionally, thanks to encapsulation, you can configure where and how your fields and methods can be accessed.

1. What is encapsulation in Java?
2. Which access modifier is the strictest?
3. What are the benefits of encapsulation?
4. Which of the following statements about encapsulation are true?

What is encapsulation in Java?

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

Which access modifier is the strictest?

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

What are the benefits of encapsulation?

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

question-icon

Which of the following statements about encapsulation are true?

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

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

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