Generics make a class, interface and, method, consider all (reference) types that are given dynamically as parameters.
This ensures type safety.
Generic class parameters are specified in angle brackets “<>” after the class name as of the instance variable.
Generic constructors are the same as generic methods.
For generic constructors after the public keyword and before the class name the type parameter must be placed.
Constructors can be invoked with any type of a parameter after defining a generic constructor.
A constructor is a block of code that initializes the newly created object.
It is an instance method with no return type.
The name of the constructor is same as the class name. Constructors can be Generic, despite its class is not Generic.
xxxxxxxxxx
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)
{
// Converting input number type to double
// using the doubleValue() method
v = t.doubleValue();
}
// Method of this class
void show()
{
// Print statement whenever method is called
System.out.println("v: " + v);
}
}
// Class 2 - Implementation class
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Display message
System.out.println("Number to Double Conversion:");
// Creating objects of type GenericConstructor i.e
// og above class and providing custom inputs to
// constructor as parameters
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();
}
}
https://www.geeksforgeeks.org/generic-constructors-and-interfaces-in-java/
xxxxxxxxxx
// Java Program to illustrate Generic interfaces
// Importing java input output classes
import java.io.*;
// An interface that extends Comparable
interface MinMax<T extends Comparable<T> > {
// Declaring abstract methods
// Method with no body is abstract method
T min();
T max();
}
// Class 1 - Sub-class
// class extending Comparable and implementing interface
class MyClass<T extends Comparable<T> >
implements MinMax<T> {
// Member variable of 'MyClass' class
T[] values;
// Constructor of 'MyClass' class
MyClass(T[] obj) { values = obj; }
// Now, defining min() and max() methods
// for MimMax interface computation
// Defining abstract min() method
public T min()
{
// 'T' is typename and 'o1' is object_name
T o1 = values[0];
// Iterating via for loop over elements using
// length() method to get 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];
// Iterating via for loop over elements using
// length() method to 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 2 - Main class
// Implementation class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Custom entries in an array
Integer arr[] = { 3, 6, 2, 8, 6 };
// Create an object of type as that of above class
// by declaring Integer type objects, and
// passing above array to constructor
MyClass<Integer> obj1 = new MyClass<Integer>(arr);
// Calling min() and max() methods over object, and
// printing the minimum value from array elements
System.out.println("Minimum value: " + obj1.min());
// printing the maximum value from array elements
System.out.println("Maximum value: " + obj1.max());
}
}
xxxxxxxxxx
ArgumentsProcessor<Double> doubleMultiplier = new ArgumentsProcessor<Double>() {
@Override
public Double process(Double arg1, Double arg2)
{
return arg1 * arg2;
}
};
System.out.println(doubleMultiplier.process(4d, 6d)); //24.0