Javatpoint Logo
Javatpoint Logo

Java Interface Generic Parameter

We can develop more adaptable and reusable interfaces in Java since an interface can include a generic type parameter. Classes, methods, and various other interfaces which may operate on a wide range of distinct data types can be defined using generic interfaces.

Declaring an arbitrary interface follows a similar syntax to doing so for a generic class. The type parameter is enclosed in angle brackets ("" and ">") and can be any acceptable Java identifier. Here's an illustration:

The interface "MyInterface" in this illustration has a type argument "T" that accepts any type. The interface specifies a single method named "doSomething" that accepts a type "T" parameter.

The generic interface can be used to build classes that implement it for various data types. For instance:

The generic interface "MyInterface" is implemented by the two classes "MyClass" and "AnotherClass" in this example. While the second class utilises the integer data type, the first class utilises the string data type.

There are various advantages to using generic interfaces. It enables us to create more flexible and reusable code. We can limit the amount of code duplication by defining interfaces that interact with a range of different data types.

It also increases the type safety of our code, which is an advantage. Generic interfaces enable us to guarantee that the types given to the methods match the anticipated data types, reducing the chance of runtime problems.

Finally, Java interface generic attributes offer an effective tool for developing adaptable and reusable interfaces. We may create classes, methods, as well as additional interfaces that can operate on a wide range of various data types by using generic interfaces. This can lessen code duplication and raise the general level of code quality. A generic interface in Java may have a number of type parameters, which may be used to define the interface's methods, variables, and other features. Angle brackets ">" are used to define the type parameters after the interface name.

Here is an example program that utilises a standard user interface and enables input of values of various data types. After performing several operations on the input values using the generic interface, the program presents the results.

GenericInterfaceExample.java

Output:

Result 1: 20
Result 2: 1.75	
Result 3: HELLO

A generic interface named "MyInterface" with the single method "performOperation" is defined in this programme. The method accepts a type "T" parameter and returns an integer of the same type.

The generic interface is then implemented using an anonymous inner class. In the "performOperation" method implementation, we use the "instanceof" operator to determine the type of the input value and then perform a different operation in accordance with the type.

We obtain input from the user for an integer, double, & string value in the main method. For each input value, we then invoke the "performOperation" function on the generic interface instance and save the outcome in an object variable. Finally, we use the "println" method to display the outcomes.


Next TopicJava Map Generic





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA