Java_Generics
Java_Generics
Here is a list of frequently asked Generics concept interview questions from Java interviews
1. What is Generics in Java? What are the advantages of using Generics?
This is one of the first interview questions asked on generics in any Java interview, mostly at beginners and
intermediate levels. Those who are coming from prior to Java 5 background know that how inconvenient it was
to store object in Collection and then cast it back to the correct Type before using it. Generics prevent those. it
provides compile time type-safety and ensures that you only insert the correct Type in collection and
avoids ClassCastException in runtime.
This was done to ensure binary compatibility with the libraries which were developed prior to Java 5. you don't
have access to Type argument at runtime and Generic type is translated to Raw type by the compiler
during runtime. you can get lot of follow up question based on this Generic interview question based upon your
response e.g. Why Generics is implemented using Type erasure or presenting some invalid generic code
which results in compiler error.
3. What is Bounded and Unbounded wildcards in Generics ?
This is another very popular Java interview questions on Generics. Bounded Wildcards are those which impose bound on
Type. there are two kinds of Bounded wildcards <? extends T> which impose an upper bound by ensuring that type must
be sub class of T and <? super T> where its imposing lower bound by ensuring Type must be super class of T. This
Generic Type must be instantiated with Type within bound otherwise it will result in compilation error. On the other hand <?
> represent and unbounded type because <?> can be replace with any Type. See more on my post differences between
Bounded and Unbounded wildcards in Generics.
4. What is the difference between List<? extends T> and List <? super T>?
This is related to previous generics interview questions, some time instead of asking what is bounded and unbounded
wildcards interviewer present this question to gauge your understanding of generics. Both of List declaration is example of
bounded wildcards, List<? extends T> will accept any List with Type extending T while List<? super T> will accept
any List with type superclass of T. for Example List<? extends Number> can
accept List<Integer> or List<Float>. see more on the above link.
5. How to write a generic method that accepts a generic argument and returns a Generic Type?
writing generic method is not difficult, instead of using raw type you need to use Generic Type like T, E or K,V which
are well-known placeholders for Type, Element and Key, Value. Look on Java Collection framework for examples of
generics methods. In simplest form a generic method would look like this:
List<Object> objectList;
List<String> stringList;
This Java Generics interview question is based on correct understanding of raw type in Generics. Any way second
difference between them is that you can pass any parametrized type to raw type List but you can not pass List<String> to
any method which accept List<Object> it will result in compilation error. Read How Generics works in Java for more
details.
List<?> listOfAnyType;
List<Object> listOfObject = new ArrayList<Object>();
List<String> listOfString = new ArrayList<String>();
List<Integer> listOfInteger = new ArrayList<Integer>();