You are here: Home / Topics / Program to explain Generic Constructor with Bounded type in Java

Program to explain Generic Constructor with Bounded type in Java

Filed under: Java on 2024-04-12 18:37:28

// Program to explain Generic Constructor with Bounded type.

class  Gen 
{
private double val;

//  Generic constructor.
<T extends Number> Gen(T arg) 
{
 val = arg.doubleValue();
}

void showValue() 
{
 System.out.println("val: " + val);
}
}

public class  GenericTest5
{
public static void main(String args[ ]) 
{
 Gen t1 = new Gen(100);
 Gen t2 = new Gen(123.5F);

 t1.showValue();
 t2.showValue();
}
}

Output:

val: 100.0
val: 123.5

About Author:
J
Java Developer     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.