# Bounded type parameters

You can limit the type of objects that can be passed as arguments to generic classes, interfaces, and methods by using bounded type parameters.

### NEED FOR BOUNDED TYPE PARAMETER:

Without a bounded type parameter (and explicit type casting), you can access only the members defined in the superclass of all classes—that is, class Object.

In the following example, the generic class Parcel won’t be able to access method getWeight() of class Gift:

![](/files/-LfKSyOEC12GF1QjkA2G)

![](/files/-LfKT-V5-ILU1x9nngje)

To access members of class Gift in Parcel, you can limit the type of objects that can be passed to class Parcel (to Gift and its subclasses) by using bounded parameters (discussed next).

### DEFINING BOUNDED TYPE PARAMETERS:

You can specify the bounds to restrict the set of types that can be used as type argu- ments to a generic class, interface, or method. It also enables access to the methods (and variables) defined by the bounds.

Let’s restrict the type of objects that can be passed to class Parcel to Gift so that the methods of class Parcel can access the methods and variables of class Gift. Because the definitions of classes Gift, Book, and Phone are the same as in the preceding section, they aren’t repeated in the following code:

![](/files/-LfKTytG3ty8zgz-zoXv)

{% hint style="success" %}

#### For a bounded type parameter, the bound can be a class, interface, or enum, but not an array or a primitive type. All cases use the keyword extends to specify the bound. If the bound is an interface, the implements keyword isn’t used.

{% endhint %}

![](/files/-LfKUA6C8apoZ1_PopbZ)

### DEFINING MULTIPLE BOUNDS:

A type parameter can have multiple bounds. The list of bounds consists of one class and/or multiple interfaces. The following example defines a generic class Parcel, the type parameter T of which has multiple bounds:

![](/files/-LfKZfRkBFgFbAWU-aFs)

{% hint style="success" %}

#### For a type parameter with multiple bounds, the type argument must be a subtype of all bounds.

{% endhint %}


---

# 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://gyansetu-core-java-for-java.gitbook.io/project/4-generics/bounded-type-parameters.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.
