VOOZH about

URL: https://www.geeksforgeeks.org/java/generic-constructors-and-interfaces-in-java/

⇱ Generic Constructors and Interfaces in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Generic Constructors and Interfaces in Java

Last Updated : 4 Aug, 2025

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:


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


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.

Comment