Generic Constructor in Java | Example Program

When we define a generic type parameter to the constructor of a class, it is called generic constructor in java.

We can use a generic constructor for both generic and non-generic classes.

The general syntax to define a generic constructor in a class is as follows:

// A generic class.
class Myclass<X>
{
// Generic constructor.
   <T> Myclass(T t) 
  {
     // constructor code.
   }
public static void main(String[ ] args)
{
// Instantiate the class.
    Myclass<Integer> obj = new Myclass<>(" ");
  }
}

In the above code, Java compiler understands that Integer is for formal type parameter X of the generic class Myclass<X>. While the type String is for the formal type parameter T of the constructor of the generic class.

Generic Constructor Example Program


1. Let’s take a very simple example program where we will create a generic constructor in the non-generic class. Look at the source code below:

Program source code 1:

// A non generic class.
public class Myclass
{
     String obj;
// Declare a generic constructor.
     <T extends Object>Myclass(String obj)
     {
	 this.obj = obj;
      }
   void displayClassName()
   {
	 System.out.println("Name of class: " +obj.getClass().getName());
   }
}
public class GenericConstructorTest {
public static void main(String[] args) 
{
   Myclass obj = new Myclass("Herry");
    obj.displayClassName();
  }
}
Output:
          Name of class: java.lang.String

In this program, we have created a non-generic class Myclass that consists of a generic constructor. The generic constructor specifies the parameter of generic type that extends Object class.

Inside the class, a method displayClassName() that displays the name of class of the object that is passed to the generic constructor.


2. Let’s create a program where we have created five classes namely, A, B, C, Myclass, and GenericConstructorTest. Although Myclass class is not a generic class that contains a generic constructor.

It specifies a generic type parameter that must be either A object or an object of its subclasses and their subclasses.

In Myclass, we will define a method displayClassName() that will display the name of class of object that is passed to the generic constructor.

Inside the main method, three Myclass objects will be created by passing the object of A, B, and C, respectively. The displayClassName() method is invoked each time and their class name is displayed on the console.

Program source code 2:

public class A {
 int x;
 A(int x) {
  this.x = x;	
 }
}
public class B extends A {
String str;
B(String str) {
    super(20);
    this.str = str;	
   }
 }
public class C extends B {
double d;
C(double d) {
	super("John");
        this.d = d;
}
}
// A non generic class.
public class Myclass
{
      A a;
// Declare a generic constructor.
     <T extends A>Myclass(A a)
     {
	 this.a = a;
     }
   void displayClassName()
   {
	 System.out.println("Name of class: " +a.getClass().getName());
   }
}
public class GenericConstructorTest {
public static void main(String[] args) 
{
   A a = new A(25);
   Myclass obj1 = new Myclass(a); // Passing the object reference of class A.
     obj1.displayClassName();
 
   B b = new B("John");
   Myclass obj2 = new Myclass(b); // Passing the object reference of class B.
    obj2.displayClassName();
 
   C c = new C(25.00);
   Myclass obj3 = new Myclass(c); // Passing the object reference of class A.
     obj3.displayClassName();
  }
}
Output:
        Name of class: genericsProgram.A
        Name of class: genericsProgram.B
        Name of class: genericsProgram.C

Hope that this tutorial has covered almost all the important points concerning with generic constructor in java with example programs. I hope that you will have understood the basic concepts of the generic constructor.

In the next tutorial, we will discuss generic interface in java with various example programs.
Thanks for reading!!!