Generic Constructors and Interfaces in Java
Last Updated :
28 Jan, 2021
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.
Implementation:
Example
Java
import java.io.*;
class GenericConstructor {
private double v;
<T extends Number> GenericConstructor(T t)
{
v = t.doubleValue();
}
void show()
{
System.out.println( "v: " + v);
}
}
class GFG {
public static void main(String[] args)
{
System.out.println( "Number to Double Conversion:" );
GenericConstructor obj1
= new GenericConstructor( 10 );
GenericConstructor obj2
= new GenericConstructor( 136 .8F);
obj1.show();
obj2.show();
}
}
|
Output
Number to Double Conversion:
v: 10.0
v: 136.8000030517578
Output explanation: Here GenericConstructor() states a parameter of a generic type which is a subclass of Number. GenericConstructor() can be called with any numeric type like Integer, Float, or Double. So, in spite of GenericConstructor() is not a generic class, its constructor is generic.
Generic Interfaces in Java are the interfaces that deal with abstract data types. Interface help in the independent manipulation of java collections from representation details. They are used to achieving multiple inheritance in java forming hierarchies. They differ from the java class. These include all abstract methods only, have static and final variables only. The only reference can be created to interface, not objects, Unlike class, these don’t contain any constructors, instance variables. This involves the “implements” keyword. These are similar to generic classes.
The benefits of Generic Interface are as follows:
- This is implemented for different data types.
- 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;
MyClass(T[] obj) { values = obj; }
public T min()
{
T o1 = values[ 0 ];
for ( int i = 1 ; i < values.length; i++)
if (values[i].compareTo(o1) < 0 )
o1 = values[i];
return o1;
}
public T max()
{
T o1 = values[ 0 ];
for ( int i = 1 ; i < values.length; i++)
if (values[i].compareTo(o1) > 0 )
o1 = values[i];
return o1;
}
}
class GFG {
public static void main(String[] args)
{
Integer arr[] = { 3 , 6 , 2 , 8 , 6 };
MyClass<Integer> obj1 = new MyClass<Integer>(arr);
System.out.println( "Minimum value: " + obj1.min());
System.out.println( "Maximum value: " + obj1.max());
}
}
|
Output
Minimum value: 2
Maximum value: 8
Output explanation: Here interface is declared with type parameter T, and its upper bound is Comparable which is in java.lang. This states how objects are compared based on type of objects. Above T is declared by MyClass and further passed to MinMax as MinMax needs a type that implements Comparable and implementing class(MyClass) should have same bounds.
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.
Share your thoughts in the comments
Please Login to comment...