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

Element: $ Number: 1

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

Element: $ Number: 1

Testing….

1

Contributed by: Poonam Tomar


Full stack Java Developer

   Buy me a coffee!

 

PayTM QR (INDIA)

Download file

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu