12.05. Bounded type parameter

Bounded Type Parameter

  • Eclipse: Oxygen
  • Java: 1.8

There could be a situation where we need the user to enter a specific type of quantity. For such cases we use generics. For example, a method that accepts only String must not be passed with numbers and, therefore, bounded type parameters are used for such purposes.

In bounded type parameter, we are bounding or restricting to specific information.

In the following code, a restriction is applied to the parameter to pass the only number as an argument. To apply restriction types of display method extends the respective classes Like <E extends Character, N extends Number>

Now passing the value other than character and number is not possible.

Output

In the following code, rule based on custom class is applied on the custom type parameter of Data class.

class Data<K extends Integer,V extends App>

Output

Contributed by: Poonam Tomar

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments