A non-generic class can be used if a specific parameterized type is provided with the generic interface. The Syntax of a java generic interface is as follows: interface interface-Name 3. Java generic interfaces do not contain any constructors or instance variables. The reference can only be created to other interfaces and not to objects, Unlike class. It helps in achieving multiple inheritances as well through a hierarchy.Ī java generic interface only includes the abstract methods and only has static and final variables. It offers various benefits like shorter and more efficient code that is also significantly faster. What differentiates Java Generic Interfaces from other interfaces is that it also deals with abstract data types. It can be used to declare a variable and can be returned from a method. A non-generic class can be used if a specific parameterized type is provided with the generic interface.Ī Java generic interface is very much like any other interface. In this case you just need to import the interface in your current class (in case it is in a different package) and then you can call the method directly. In Java 8, you can implement the method body inside the interface itself, and using the static access modifier for it. The generic class can have other parameterized type parameters. The implementing class will give it the body that you need. The implementing class MUST also be generic. Rules to Implement a Java generic Interface.Remove the Formal Type Parameters (not recommended) Ways to implement a Java Generic Interface.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |