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
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/