Generics interview question and answer in Java

In this article, we will cover some of the interview questions with their justification on Generics concepts introduced in Java 1.5 version

These are most frequently asked interview question from Generics in Java

Read Generics concepts in detail

 

Q) What is the main objective of Generics ?

  • To provide type-safety and
  • To resolve type-casting problems

Q) What is the alternative to Generics for providing type-safety ?

  • Arrays, which stores same-type of Objects within its capacity
  • but they are fixed in size

Q) What is need of introducing Generics, when Arrays already present in Java ?

  • Prior to Java 1.5 version release, Collection aren’t type-safe
  • So, we can turn towards Arrays for type-safety
  • But they are fixed in size, doesn’t allows to grow when more number of elements need to be added and similarly shrinks when elements are removed from Arrays
  • Key advantage: In addition to this, generics provide type-safety as well as removes overhead of explicit type-casting problem

Q) In which version of Java, Generics introduced ?

  • Java 1.5 version
  • codename called Tiger

Q) What is base-type and type-parameter T in Generics ?

  • In the below screen-capture, ArrayList is base-type and String is Type-parameter

Q) What is T (Type-parameter) in Generics ?

  • It is the notation to indicate that, it is generics class to accept any valid Java reference-types
  • Type parameter T can be replaced by wrapper-types or reference-types
  • But not allowed to replace by primitive-types like int, float, double. etc.
  • Syntax: methodName(ArrayList<T>);

Q) Write general syntax for Generics class ?

  • Syntax: <access-modifier> class <class-name> <Type-Parameter> { // declaration }
  • Example: public class GenericsClass {variable & method declarations}

Q) Whether it is mandatory to use T for type-parameter while defining Generics classes or methods ?

  • No, it isn’t mandatory to use T for type-parameter
  • We can use any valid identifier in Java like A or X, ABC, etc.
  • But, it is convention to use T, as first-letter of Type-parameter

Q) Whether type-parameter T can be used along with Generics method ?

  • Yes, we can use type-parameter T along with Generics method
  • Very similar to Generics classes

Q) Where type-parameter T need to be defined in Generics method ?

  • Just before the return type
  • It can be unbounded with just Type-parameter T
  • Or it can be bounded using extends keyword

Q) Whether it is allowed to define more than one Type-parameter ?

  • Yes, it is possible to defined multiple type-parameter while defining Generics classes

Q) Whether primitive-type can be used for type-parameter T in Generics ?

  • No, primitive-types aren’t allowed to replace type-parameter T
  • Only wrapper-types or reference-types like classes or interfaces can be used to replace Type-parameter

Q) Whether inheritance relationship can be applied to Generics ?

  • No, it isn’t possible to apply inheritance relationship
  • In case, if we apply inheritance relationship then compiler throws errors, as shown in the below screen-capture
  • Compile-time error: Type mismatch: cannot convert from GenericsClass<String> to GenericsClass<Object>

Q) What are bounded-types in Generics ?

  • Writing Type-Parameter T without any bounds while defining Generics class or method allows to accept any reference-types
  • But to define bounds for Generics class & methods, we can use extends keyword to put limit to accept certain range of reference-types
  • For example, defining <T extends Number> accepts only Number class or one of its sub-classes
  • Trying to pass any other class or reference-type results in compile-time error

Q) How to define bounded-type for Generics class & methods ?

  • Bounded-type in Generics allows to put a limit or range of reference-types to be accepted
  • To define bound, we can use extends keyword
  • Synatx: <T extends referenceType>
  • This referenceType can be either class or interface
  • If referenceType is a class, then Type-parameter T can be replaced by either class or one of its sub-classes
  • Similarly, if referenceType is an interface, then Type-parameter T can be replaced by either interface or one of its implementation classes
  • Note: Bounds can be applied to both Generics class as well as methods

Q) State various condition to define bounded-types ?

  • Maximum of one class followed by any number of interfaces
  • Use ampersand & sign to separate class & interfaces
  • Sequence is very important, as class should be first and then followed by interfaces
  • Defining more than one classes for bound-type results in compile-time error

Q) What are wildcards arguments in Generics ?

  • Question mark (?) is known as wildcard
  • It is used to represent unknown type
  • There are 3 different cases for wildcards
  • Those are unbounded, upper-bounded & lower-bounded

Q) Whether wildcard arguments (?) allowed for Generics classes and method ?

  • Wildcard arguments applicable only for methods
  • It isn’t applicable for classes

Q) Explain upper-bounded wildcard w.r.t Generics method ?

  • We can apply upper-bound to generics method using extends keyword
  • Syntax: <? extends referenceType>
  • This ReferenceType can be either class or interface
  • If referenceType is a class, then wildcard (?) can be replaced by either class or one of its sub-classes
  • Similarly, if referenceType is an interface, then Type-parameter T can be replaced by either interface or one of its implementation classes

Q) Explain lower-bounded wildcard w.r.t Generics method ?

  • We can apply lower-bound to generics method using super keyword
  • Syntax: <? extends referenceType>
  • This ReferenceType can be either class or interface
  • If referenceType is a class, then wildcard (?) can be replaced by either class or its super-classes
  • Similarly, if referenceType is an interface, then Type-parameter T can be replaced by either interface or super-class of implementation classes of referenceType
  • Note: super keyword strictly applicable for Generics method when used with wildcard arguments

 Q) Whether it is possible to define generic method inside Non-generic class ?

  • Yes, it is possible to declare Generics method inside Non-Generic class

Q) Whether it is allowed to define generic method as static ?

  • Yes, it is allowed to define Generics method as static

 Q) Write definition for generic method ?

  • Syntax: <access-modifier> <Type-parameter> <return-type> <method-name>();
  • Example: public <T extends Number> void method1();

 Q) Where exactly Type-Parameter need to be specified, when declaring generic method ?

  • Define Type-Parameter T just before return-type, in method signature

 Q) What are the keywords applicable for Generics method ?

  • Using extends keyword, we can define upper-bound for Generics method
  • It can be used with Type-parameter as well as with wildcard arguments
  • Using super keyword, we can define lower-bound for Generics method
  • It can be used only with wildcard arguments

 Q) Write definition for generic constructor ?

  • Yes, it is possible to declare Generics constructor, as shown in the below screen-capture

 Q) Whether it is allowed to define generic constructor ?

  • Yes, it is allowed to define Generics constructor, as shown in the below screen-capture
  • Define Type-Parameter T in between access-modifier and constructor-name
  • If access-modifier isn’t specified then define Type-Parameter T just before constructor-name
  • It is also allowed to apply bounds to Type-Parameter T

Q) Where exactly type-parameter need to be specified, when declaring generic constructor ?

  • Just before Generics constructor-name

 Q) Whether it is possible to instantiate Type-parameter <T> ?

  • No, it isn’t possible to instantiate Type-parameter
  • As Generics information present in Generics class get type-erased during compilation
  • And it isn’t available at execution-time

 

Hope, you found this article very helpful. If you have any suggestion or want to contribute or tricky situation you faced during Interview hours, then share with us. We will include that code here

 

References:

 

Happy Coding !!
Happy Learning !!

ArrayList v/s ArrayList
Wildcard arguments in Generics
  • Adil Khan

    Thanks for sharing…. its always deep learning with you 🙂