Java-Generic Constructors

Generic Constructors It is possible for constructors to be generic, even if their class is not. For example, in the following program, NumberToInt constructor specifies a parameter of a generic type, which must be a subclass of Number, NumberToInt constructor can be called with any numeric type, including Integer, Float, or Double. Therefore, even though … Read more…

Generic Interfaces

Java-Generic Interfaces

Generic Interfaces Generics also work with interfaces. Thus, you can also have generic interfaces. Generic interfaces are specified just like generic classes. For example : The MyInterface is a generic interface that declares the method called myMethod( ). In general, a generic interface is declared in the same way as is a generic class. In this case, the … Read more…

Bridge Method

Java-Bridge Method

Bridge Method Sometimes, the compiler will need to add a bridge method to a class to handle situations in which the type erasure of an overriding method in a subclass does not produce the same erasure as the method in the superclass. In this case, a method is generated that uses the type erasure of … Read more…

Overriding Methods

Java-Overriding Methods

Overriding Methods A method in a generic class can be overridden just like any other method. For example, consider the following program in which the method getT( ) is overridden. As the output confirms, the overridden version of getT( ) is called for object of type GenTwo, but you can call the superclass version via GenOne object. … Read more…

Generic Superclass

Java-Generic Superclass

Generic Superclass Generic classes can be part of a class hierarchy in just the same way as a non-generic class. Thus, a generic class can act as a superclass or be a subclass. The key difference between generic and non-generic hierarchies is that in a generic hierarchy, any type arguments needed by a generic superclass … Read more…