Java - Generics - Interview Questions

Java generics was first introduced into the Java programming language in J2SE 5.0. Generics extends the Java type capabilities by enabling a Java type or method to operate on objects of different types. Generics also provides compile time safety, by identifying invalid types and throwing errors at compile time.

Important keywords are provided at the end of the questions. Review them, make them a part of Java generics vocabulary, and talk about them confidently during your interview process.

What do you understand by Generics in Java programming language? What are the benefits of Generics.

 FAQKey Concept

Generics in Java programming language enable Java types, i.e. classes and interfaces, to be declared as parameters to classes, interfaces and methods. Generics provide a way to reuse the same code with different inputs, i.e different types. Code that uses generics has many advantages over code that is non-generic.

  • Compile time type checks - The Java compiler applies type checking to generic code at compile time and throes errors for type safety violations.
  • Eliminates implicit casting - Java code that uses generics do not need explicit casting, where as the code that is non-generic requires explicit casting.
  • Implementation of generic algorithms - By using generics, programmers can develop generic algorithms designed to work on collections of different types that are type safe and easier to use.

What are generic types?

 FAQKey Concept

A generic type is a generic class or interface that is parameterized over types.

Declaration of generic types - A generic class is declared using the format 'class MyClass {...}'. The angle brackets <> is the type parameter section and specifies the type parameters T1, T2 ... for the generic class.

Instantiation of generic types - You instantiate a generic class using the new keyword as usual, but pass actual types within the parameter section. Example - 'MyClass myClass = new MyClass();'. In Java 7 and later you can eliminate the type and just have the angle brackets <>. Example - 'MyClass myClass = new MyClass<>();'. The empty angular brackets <> syntax is commonly referred to as the Diamond.

What are generic methods?

 FAQKey Concept

Generic methods are methods that are declared with type parameters. Generic methods can be static, non-static or constructors.

Declaration of generic methods -

Instantiation of generic methods

What are bounded type parameters?

 FAQKey Concept

Bounded type parameters enable you to restrict the types that you can use as arguments for a parameterized type. For example if a method acts only on numbers, then you can use bounded parameters to specify that the method accepts only instances of Number or its sub-classes.

How do you create sub-classes of generic classes?

 FAQKey Concept

Similar to regular classes and interfaces, you can create sub-types of generic classes or interfaces by extending or implementing from the generic classes or interfaces.

For example, in Java collections API, ArrayList implements List and List implements Collections. The sub-type relation is preserved as long as the type argument does not vary. So, ArrayList implements List and List implements Collections.

Java Interview Guide has over 250 REAL questions from REAL interviews. Get the guide for $15.00 only.
 
BUY EBOOK
 

Are Generics applied at compile time or run time? What is type erasure?

 FAQKey Concept

Generics are applied at compile time provider stronger type checks. Once the type checks are complete, the compiler erases the type check code so that generics do not incur runtime over head...

*** See complete answer in the Java Interview Guide.

Can Java generics be applied to primitive types?

 FAQKey Concept

No Java generics cannot be applied to primitive types....

*** See complete answer in the Java Interview Guide.

Can you create instances of generic type parameters?

 FAQKey Concept

No you cannot create instances of generic type parameters...

*** See complete answer in the Java Interview Guide.

What do you understand by wildcards in generics?

 FAQKey Concept

The question mark (?) is termed as wildcard in generics code...

*** See complete answer in the Java Interview Guide.

Java Interview Guide has over 250 REAL questions from REAL interviews. Get the guide for $15.00 only.
 
BUY EBOOK
 
 
Java Interview Guide

$15.00

BUY EBOOK
  SSL Secure Payment
Java Interview Quesiuons - Secure Payment
Java Interview Guide

$15.00

BUY EBOOK
  SSL Secure Payment
Java Interview Quesiuons - Secure Payment
 

Java - Interview Questions

Java - Object Oriented ProgrammingJava - Objects & ClassesJava - Data TypesJava - VariablesJava - StringsJava - ArraysJava - CollectionsJava - ReflectionJava - Lambda ExpressionsJava - StreamsJava - GenericsJava - ExceptionsJava - IOJava - ThreadsJava - ConcurrencyJava - JDBCJava - NetworkingJava - SecurityJava - JVM InternalsJava - PerformanceJava - New in Java 8Java - New in Java 9Java - New in Java 10Java - New in Java 11
 
MASTER Java  

Top ranked courses to help you master Java skills.
Java Programming Masterclass

iconicon

Offered By - Tim Buchalka
Platform - Udemy
Rating - * * * * *
Students Enrolled - 575,000 +

RECOMMENDED RESOURCES
Behaviorial Interview
Top resource to prepare for behaviorial and situational interview questions.

STAR Interview Example