Java | Generics | Interview Questions


Generics Introduced in Java 5, Generics add a great deal of flexibility to Java. Interview questions related to Generics - How to create a Generic class, method. What are Generics? Why do we need Generics? Can you give an example of how Generics make a program more flexible? How do you declare a Generic Class? What are the restrictions in using generic type that is declared in a class declaration? How can we restrict Generics to a subclass of particular class? How can we restrict Generics to a super class of particular class? Can you give an example of a Generic Method?

  1. What are Generics?
    The Java Generics programming is introduced in J2SE 5 to deal with type-safe objects.
    Generics are use to create generic classes and generic methods which can be used in defferent types(classed).


     

  2. Why do we need Generics? Can you give an example of how Generics make a program more flexible?
    Before generics, we can store any type of objects in collection i.e. non-generic. Now generics, forces the java programmer to store specific type of objects.


     

  3. How do you declare a Generic Class?
    Note the declaration of class:Instead of T, We can use any valid identifier.

    class MyGeneric<T>
  4. What are the restrictions in using generic type that is declared in a class declaration?
    If a generic is declared as part of class declaration, it can be used any where a type can be used in a class - method (return type or argument), member variable etc. For Example: See how T is used as a parameter and return type in the class MyGeneric.


     

  5. How can we restrict Generics to a subclass of particular class?
    In MyGeneric, Type T is defined as part of class declaration. Any Java Type can be used a type for this class. If we would want to restrict the types allowed for a Generic Type, we can use a Generic Restrictions. Consider the example class below: In declaration of the class, we specified a constraint "T extends Number". We can use the class MyListRestricted with any class extending (any sub class of) Number - Float, Integer, Double etc.

    class MyListRestricted<T extends Number> {
        private List<T> values;
    
        void add(T value) {
            values.add(value);
        }
    
        void remove(T value) {
            values.remove(value);
        }
    
        T get(int index) {
            return values.get(index);
        }
    }
    
    MyListRestricted<Integer> restrictedListInteger = new MyListRestricted<Integer>();
    restrictedListInteger.add(1);
    restrictedListInteger.add(2);
    
  6. How can we restrict Generics to a super class of particular class?
    In MyGeneric, Type T is defined as part of class declaration. Any Java Type can be used a type for this class. If we would want to restrict the types allowed for a Generic Type, we can use a Generic Restrictions. In declaration of the class, we specified a constraint "T super Number". We can use the class MyListRestricted with any class that is a super class of Number class.

     

  7. Can you give an example of a Generic Method?
    A generic type can be declared as part of method declaration as well. Then the generic type can be used anywhere in the method (return type, parameter type, local or block variable type).

    Consider the method below:

      static <X extends Number> X doSomething(X number){
            X result = number;
            //do something with result
            return result;
        }