------------------------------------------------------------------------------------------
Generics
------------------------------------------------------------------------------------------
Very good collection of Q&A about Java Generics can be found
http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html
Questions
-------------------------------
1. What is Generics in Java ? What are advantages of using Generics?
They allow "a type or method to operate on objects of various types while providing compile-time type safety." A common use of this feature is when using a Java Collection that can hold objects of any type, to specify the specific type of object stored in it.
A class is generic if it declares one or more type variables.
2. How Generics works in Java ? What is type erasure ?
The type safety is ensured during the compile time. In run time the information about type arguments is erased. The type arguments are replaced by their narrowest superclass. For unbounded parameters -- Object. For bounded -- the declared bound.
3. What is Bounded and Unbounded wildcards in Generics ?
The wildcard type is signified by "?" in Java. Classes parametrized by wildcard are usually read as "class of some type", e.g. List is read List of some type.
A wildcard parameterized type is an instantiation of a generic type where at least one type argument is a wildcard. Examples of wildcard parameterized types are Collection, List, Comparator and Pair.A wildcard parameterized type denotes a family of types comprising concrete instantiations of a generic type. The kind of the wildcard being used determines which concrete parameterized types belong to the family. For instance, the wildcard parameterized type Collection denotes the family of all instantiations of the Collection interface regardless of the type argument. The wildcard parameterized type List denotes the family of all list types where the element type is a subtype of Number. The wildcard parameterized type Comparator is the family of all instantiations of the Comparator interface for type argument types that are supertypes of String.
A wildcard parameterized type is not a concrete type that could appear in a new expression. A wildcard parameterized type is similar to an interface type in the sense that reference variables of a wildcard parameterized type can be declared, but no objects of the wildcard parameterized type can be created. The reference variables of a wildcard parameterized type can refer to an object that is of a type that belongs to the family of types that the wildcard parameterized type denotes.
4. What is wildcard prameter
In generic code, the question mark (?), called the wildcard, represents an unknown type. The wildcard can be used in a variety of situations: as the type of a parameter, field, or local variable; sometimes as a return type (though it is better programming practice to be more specific). The wildcard is never used as a type argument for a generic method invocation, a generic class instance creation, or a supertype.
5. What is difference between List and List ?
The first list may contain elements of type T or any of its subtypes.
The second list may contain elements of type T or any of its supertypes.
For any type X such that X is a subtype of T or T itself
List is a subtype of List
For any type Y such that Y is a supertype of T or T itself
List is a subtype of List
List is supertype of any parametrized list.
6. How to write a generic method which accepts generic argument and return Generic Type?
Generic method in a generic class
public class SomeClass{
private T value;
// T is already declared
// since it's type parameter of the class
public T doSomething(){...}
// U has to be declared since it's not type parameter of the class
public List createSingleElementList(U arg){
List result = new ArrayList();
result.add(arg);
return result;
}
}
Static generic method
public static T doSomething(T value){...}
7. How to write parametrized class in Java using Generics ?
class SomeClass extends SomeSuperClass implements SomeInterface
class SomeClass>
class SomeClass
8. Can you pass List to a method which accepts List