# 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:

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LfKSY5ITijrC5HdRZsp%2F-LfKSyOEC12GF1QjkA2G%2FScreenshot%202019-05-20%20at%207.09.05%20PM.png?alt=media\&token=bc5a09a0-bea3-4fe6-bce2-12714418819f)

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LfKSY5ITijrC5HdRZsp%2F-LfKT-V5-ILU1x9nngje%2FScreenshot%202019-05-20%20at%207.09.11%20PM.png?alt=media\&token=77c79b5b-ab19-4818-b474-928c9669f0ce)

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:

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LfKSY5ITijrC5HdRZsp%2F-LfKTytG3ty8zgz-zoXv%2FScreenshot%202019-05-20%20at%207.13.22%20PM.png?alt=media\&token=1d92683e-5cae-4981-9800-f94664dc99d4)

{% 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 %}

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LfKSY5ITijrC5HdRZsp%2F-LfKUA6C8apoZ1_PopbZ%2FScreenshot%202019-05-20%20at%207.14.29%20PM.png?alt=media\&token=15b34093-b88f-4f98-9582-f3bdd9d91b43)

### 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:

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LfKSY5ITijrC5HdRZsp%2F-LfKZfRkBFgFbAWU-aFs%2FScreenshot%202019-05-20%20at%207.38.26%20PM.png?alt=media\&token=3520779e-dc87-4974-b915-86150db186b0)

{% hint style="success" %}
#### For a type parameter with multiple bounds, the type argument must be a subtype of all bounds.
{% endhint %}

