# Использование обобщений

Обобщения - это параметризованные типы. С их помощью можно объявлять классы, интерфейсы и методы, где тип данных указан в виде параметра. Обобщения добавили в язык безопасность типов.

```
class Gen<T> {
    T ob; // объявление объекта типа T

    // Передать конструктору ссылку на объект типа T
    Gen(T o) {
        ob = o;
    }

    // Вернуть ob
    T getob() {
        return ob;
    }

    // Показать тип T
    void showType() {
        System.out.println("Тип T: " + ob.getClass().getName());
    }
}

// Код для кнопки
// Работаем с обобщённым классом
// Создаём Gen-ссылку для Integer
Gen<Integer> iOb;

// Создаём объект Gen<Integer>
iOb = new Gen<Integer>(77);

// Показать тип данных, используемый iOb
iOb.showType();

// Получить значение iOb
int value = iOb.getob();
System.out.println("Значение " + value);

// Создадим объект Gen для String
Gen<String> strOb = new Gen<String>("Обобщённый текст");

// Показать тип данных, используемый strOb
strOb.showType();

// Получить значение strOb
String str = strOb.getob();
System.out.println("Значение: " + str);
```

Вопрос 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://comaqa.gitbook.io/java-automation/oop-v-java/ispolzovanie-obobshenii.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.
