# Generics and Wildcards

Сейчас мы приступим к изучению одной из самых сложных частей в обобщениях - wildcards.

Вспомним как мы создавали стандартный generic класс:

```
    class ArrayList<T> {
        ...
    }
```

в данном случае T - это type parameter класса ArrayList.

Создание экземпляра такого класса выглядит следующим образом:

```
ArrayList<Number> arrayList = new ArrayList<Number>();
```

Все четко, при объявлении параметр T задаете как Number.

Теперь вы хотите написать функцию, которая суммирует все элементы коллекции:

```
public static double sum(ArrayList<Number> numbers){
    double summ = 0.0;
    for(Number curNumber : numbers)
    summ += curNumber.doubleValue();
    return summ;
}
```

Но когда мы захотим передать в это функцию коллекцию вида ArrayList, то у нас возникнут проблемы. Хотя на первый взгляд все выглядит правильно. Класс у нас generic, integer вроде бы extends Number, но что-то не так. Как же решается эта проблема? Для этого и существуют wildcards. Посмотрим как это работает на примере:

```
public static double sum(ArrayList<? extends Number> numbers){
    ...
}
```

Т.е. метод принимает тип ArrayList который параметризован Number или наследником Number.


---

# 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/generics-and-wildcards.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.
