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

Зміст курсу

Java OOP

Java OOP

1. How to Work With IDE?
2. OOP
3. Interface

bookOOP 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.

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:

  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:

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?

What is encapsulation in Java?

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

Which access modifier is the strictest?

Which access modifier is the strictest?

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

What are the benefits of encapsulation?

What are the benefits of encapsulation?

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

Which of the following statements about encapsulation are true?

Which of the following statements about encapsulation are true?

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

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

Як ми можемо покращити це?

Дякуємо за ваш відгук!

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