Open In App

Generic Constructors and Interfaces in Java

Last Updated : 04 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Generics allow classes, interfaces and methods to operate on objects of various types while ensuring type safety. Type parameters are specified in angle brackets (<>) and enable code reusability without sacrificing type checks at compile time.

Generic Constructor

Generic constructors define their type parameter before the constructor name, even if the class itself is not generic. It can accept arguments of any type and is mainly used to create flexible and type-safe object initialization. 

Implementation: 

Java
import java.io.*;
class GenericConstructor {
    // Member variable of this class
    private double v;

    // Constructor of this class where T is typename and t is object
    <T extends Number> GenericConstructor(T t)
    {
        
        v = t.doubleValue();
    }
    void show()
    {
        System.out.println("v: " + v);
    }
}

class GFG {
    // Main driver method
    public static void main(String[] args)
    {
        // Display message
        System.out.println("Number to Double Conversion:");

        GenericConstructor obj1
            = new GenericConstructor(10);
        GenericConstructor obj2
            = new GenericConstructor(136.8F);

        // Calling method - show() on the objects using the dot operator
        obj1.show();
        obj2.show();
    }
}

Output
Number to Double Conversion:
v: 10.0
v: 136.8000030517578

Explanation:

  • The GenericConstructor class has a generic constructor that accepts any type extending Number.
  • Inside the constructor, the numeric value is converted to double using t.doubleValue() and stored in v.
  • The show() method prints the stored double value.
  • In the main() method, two objects are created using an int and a float value.
  • The output displays the double equivalents of the input numbers.

Generic Interfaces

Generic interfaces define abstract data types that operate on various object types while ensuring type safety. They support multiple inheritance, contain only abstract methods and constants and use the implements keyword. Like generic classes, they enable code reusability but cannot be instantiated directly.

The benefits of Generic Interface are as follows:

  1. This is implemented for different data types.
  2. It allows putting constraints i.e. bounds on data types for which interface is implemented.

Syntax:

interface interface-Name < type-parameter-list > {
//....
} class class-name <type-parameter-list> implements interface-name <type-arguments-list> {
//...
}

Implementation: The following example creates an interface 'MinMax' which involves very basic methods such as min(), max() just in order to illustrate as they return the minimum and maximum values of given objects.

Example

Java
import java.io.*;
interface MinMax<T extends Comparable<T> > {
    T min();
    T max();
}

class MyClass<T extends Comparable<T> >
    implements MinMax<T> {

    T[] values;

    // Constructor of 'MyClass' class
    MyClass(T[] obj) { values = obj; }


    // Defining abstract min() method
    public T min()
    {
        // 'T' is typename and 'o1' is object_name
        T o1 = values[0];

    // access of minimum element
        for (int i = 1; i < values.length; i++)
            if (values[i].compareTo(o1) < 0)
                o1 = values[i];

        // Return the minimum element in an array
        return o1;
    }

    // Defining abstract max() method
    public T max()
    {
        // 'T' is typename and 'o1' is object_name
        T o1 = values[0];

       // Get access of minimum element
        for (int i = 1; i < values.length; i++)
            if (values[i].compareTo(o1) > 0)
                o1 = values[i];

        // Return the maximum element in an array
        return o1;
    }
}

class GFG {
    // Main driver method
    public static void main(String[] args)
    {
        // Custom entries in an array
        Integer arr[] = { 3, 6, 2, 8, 6 };

        MyClass<Integer> obj1 = new MyClass<Integer>(arr);

        System.out.println("Minimum value: " + obj1.min());

        // printing the maximum value from array elements
        System.out.println("Maximum value: " + obj1.max());
    }
}

Output
Minimum value: 2
Maximum value: 8

Explanation:

  • A generic interface MinMax<T> defines two abstract methods: min() and max() for finding minimum and maximum values.
  • The class MyClass<T> implements this interface using a generic array and ensures the type T extends Comparable for comparison.
  • The min() method loops through the array to find and return the smallest element.
  • The max() method loops through the array to find and return the largest element.
  • In main(), an Integer array is passed to MyClass and it prints the minimum and maximum values.

Note: Once a bound is established, it is not necessary to state it again in implements clause. If a class implements generic interface, then class must be generic so that it takes a type parameter passed to interface.


Explore