A Java generic interface is very much like any other interface. It can be used to declare a variable and can be returned from a method. It can also be passed as an argument. What differentiates Java Generic Interfaces from other interfaces is that it also deals with abstract data types.
Table of Contents
It offers various benefits like shorter and more efficient code that is also significantly faster. It helps in achieving multiple inheritances as well through a hierarchy.
A java generic interface only includes the abstract methods and only has static and final variables. The reference can only be created to other interfaces and not to objects, Unlike class. Java generic interfaces do not contain any constructors or instance variables.
The Syntax of a java generic interface is as follows:
interface interface-Name < type-parameter-list > { .... .... .... } class class-name <type-parameter-list> implements interface-name <type-arguments-list> { ... ... ... }
The following example creates an interface called ‘SmallestLargest’ which has some very basic methods like findSmallest() and findLargest() that returns the smallest and largest values from the given objects. The ‘SmallestLargest’ interface has a formal type parameter T.
The other parts of the interface use T just like they would use an actual type such as String.
See the code below:
1. import java.io.*; 2. interface SmallestLargest<T extends Comparable<T> > { 3. 4. T findSmallest(); 5. T findLargest(); 6. } 7. 8. class class1<T extends Comparable<T> > 9. implements SmallestLargest<T> 10. T[] values; 11. 12. // class1 Constructor 13. Class1(T[] obj) { values = obj; } 14. 15. // Definition of findSmallest() and findLargest() methods 16. 17. public T findSmallest() 18. { 19. T obj1 = values[0]; 20. for (int i = 1; i < values.length; i++) 21. if (values[i].compareTo(obj1) < 0) 22. obj1 = values[i]; 23. return obj1; 24. } 25. 26. public T findLargest() 27. { 28. 29. T obj11 = values[0]; 30. for (int i = 1; i < values.length; i++) 31. if (values[i].compareTo(obj1) > 0) 32. obj1 = values[i] 33. return obj1; 34. } 35. } 36. 37. class myClass { 38. 39. public static void main(String[] args) 40. { 41. Integer myArr[] = { 88, 21, 63, 51, 95, 23, 97 }; 42. Class1<Integer> obj1 = new MyClass<Integer>(myArr); 43. System.out.println("Smallest value is " + obj1.findSmallest()); 44. System.out.println("Largest value is " + obj1.findLargest()); 45. } 46. }
Output Smallest value is 21 Largest value is 97
Following are the three basic ways you can use to implement Java Generic Interfaces:
One of the methods is to create a generic class that implements the Java generic interface. The class definition will use the same formal type parameters two times, First, after the class name and then after the interface name that is implemented.
See the following demonstration where <T> is used twice:
1. public class UsingAGenericClass<T> implements JGI<T> 2. { 3. private String address; 4. private T value; 5. 6. @Override 7. public void setValue(U t, String AddressCode) 8. { 9. value = t; 10. address = AddressCode; 11. } 12. 13. @Override 14. public T getValue() 15. { 16. return value; 17. } 18. 19. @Override 20. public String getAddress() 21. { 22. return address; 23. } 24. }
By using a class, the actual type can be used instead of using formal type parameters. For instance, use can use <Data> instead of <T>. Despite using a Java Generic Interface, it allows using the formal type parameter that is a non-generic type.
1. import ocp_java.ocp_java.generics.Data; 2. 3. public class DataValue implements JGI<Data> 4. { 5. private String address; 6. private Data value; 7. 8. @Override 9. public void setValue(Data t, String AddressCode) 10. { 11. value = t; 12. address = addressCode; 13. } 14. 15. @Override 16. public Data getValue() 17. { 18. return value; 19. } 20. 21. @Override 22. public String getAddress() 23. { 24. return address; 25. } 26. }
Removing or ignoring the formal type parameters is an ex. This is a way around to get to use the features of Java generic Interfaces. This method has only made the list because if you are still working with JDK version 1.4 or lower then this is the only option you have as Java Generics were not available before JDK version 1.5.
Consider the following codes, it is the same example as before but with formal parameters removed:
1. public class RemoveFormalTypeParameters implements JGI 2. { 3. private String address; 4. private Obj value; 5. 6. @Override 7. public void setValue(Obj v, String addressCode) 8. { 9. value = v; 10. address = addressCode; 11. } 12. 13. @Override 14. public Object getValue() 15. { 16. return value; 17. } 18. 19. @Override 20. public String getAddress() 21. { 22. return Address; 23. } 24. }
The class RemoveFormalTypeParameters implements the JGI interface without using a formal type parameter. As a result, the class will use the Object type for the value variable.
If the Object type is used in an Interface definition, it will eventually be very difficult to debug and maintain this code later. Despite it being doable, the best approach is obviously to use Java Generic Interfaces to avoid any problems later.
If the implementing class of the Java generic interface is not generic, it will result in a compile-time error due to the unknown parameterized type.
See this example of a sample generic Interface:
1. public interface JavaGenericInterface <T> { 2. void setName(T name); 3. public T getName(); 4. }
This is the Implementing Class:
1. public class GenericClass <T> implements JavaGenericInterface <T>{ 2. 3. T name; 4. @Override 5. public void setName(T name) { 6. this.name = name; 7. } 8. 9. @Override 10. public T getName() { 11. return name; 12. } 13. 14. public static void main(String[] args) { 15. GenericClass<String> user = new GenericClass<>(); 16. user.setName("Shaharyar Malik Lalani"); 17. System.out.println("My name is "+ user.getName()); 18. } 19. } 20.
An implementing generic class can also have more than one parameterized type arguments.
See the example below:
1. public class GenericClass <X, T> implements GenericInterface <X>{ 2. 3. X name; 4. T age; 5. 6. @Override 7. public void setName(X name) { 8. this.name = name; 9. } 10. 11. @Override 12. public X getName() { 13. return name; 14. } 15. 16. public void setAge(T age){ 17. this.age = age; 18. } 19. 20. public T getAge(){ 21. return age; 22. } 23. 24. public static void main(String[] args) { 25. GenericClass <String, Integer> user = new GenericClass<>(); 26. user.setPersonName("Shaharyar Malik Lalani"); 27. user.setAge(25); 28. System.out.println("My name is "+ user.getName()+ ", "+ “I am “ + user.getAge()+ " years old."); 29. } 30. }
1. public class GenericClass implements GenericInterface <String>{ 2. 3. String name; 4. @Override 5. public void setName(String name) { 6. this.name = name; 7. } 8. 9. @Override 10. public String getName() { 11. return name; 12. } 13. 14. public static void main(String[] args) { 15. GenericClass user = new GenericClass(); 16. user.setName("Shaharyar Malik Lalani"); 17. System.out.println("My name is "+ user.getName()); 18. } 19. }
In this article, we discussed how to implement a Java Generic Interface.
Also Read: Generate Random String in Java
The use of generic interfaces in java is a recommended practice as it provides less boilerplate code and increases the efficiency of your program.
Create a free profile and find your next great opportunity.
Sign up and find a perfect match for your team.
Xperti vets skilled professionals with its unique talent-matching process.
Connect and engage with technology enthusiasts.
© Xperti.io All Rights Reserved
Privacy
Terms of use