# 캡슐화

![http://www.rssofttech.com/encapsulation-in-java-how-to-master-oops-with-encapsulation/](/files/-MGiRmRARJjOEdxfo18F)

## 캡슐화란

캡슐화는 객체가 내부적으로 기능을 어떻게 구현하지는지를 감추는 것이다.

단순히 프라이빗한 데이터를 직접 접근하지 못하게 하기 위해 사용하는 것이 아닌 내부적인 기능에 초점을 맞추어야 한다. 단순히 데이터를 접근하기 위한 용도로 캡슐화를 사용한다면 객체에 대한 책임이 분산될 수 있다.

### 캡슐화를 하기 위한 두가지 규칙이 있다.

#### 묻지 말고 시켜라

참조한 객체의 데이터에 대한 로직이 필요할 경우에 직접 구현하지 말고 구현된 데이터를 요청하여 데이터에 대한 책임을 전가 시킬 수 있다.

#### 데미테르의 법칙

1 뎁스만 사용할 뿐 2 뎁스로 사용하지 말아야 한다. 이것은 묻지 말고 시켜라의 연장선으로 참조하는 객체가 가지고 있는 데이터를 참조하는 사용자 입장에서는 알 필요가 없고 데이터를 사용하여 데이터의 내부 기능을 사용하게 되면 내부 기능의 변화에 범위가 외부에도 노출되어 의존성이 낮아 진다.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://incheol-jung.gitbook.io/docs/q-and-a/architecture/undefined-1.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
