Using a Generic Class

This section describes 4 basic areas that require attention with using a generic class: calling the constructor, declaring a reference variable, passing an argument to methods and handling the returning reference from methods.

When using a generic class, you need to pay attention to 4 basic areas:

1. Calling the constructor of a generic class - When calling a constructor of a generic class, you need to provide a specific type to the type parameter enclosed in angle brackets after the class name. Examples are listed below:

   new java.util.Stack<double[]>();
   new java.util.ArrayList<String>();
   new java.util.Vector<Number>(200);

2. Declaring a variable for a generic class type - When declaring a variable for a generic class type, you need to provide a specific type to the type parameter enclosed in angle brackets after the class name. Examples are listed below:

   java.util.Stack<double[]> stackForDoubleArray; 
   java.util.ArrayList<String> arrayListForString;
   java.util.Vector<Number> vectorForNumber;

3. Passing an argument to a method that uses the type parameter - When calling a method in a generic class that uses a type parameter as an argument, you need to pass a reference that is compatible with the specific type provided to the generic class. Examples are listed below for the method "boolean add(E e)" in the class "java.util.Vector<E>":

   java.util.Vector<Number> verctorForNumber;
   ...
   vectorForNumber.add(new Double(9.99)); 
      // allowed - Double is compatible with Number 
   
   vectorForNumber.add(new String("9.99"));
      // not allowed - String is not compatible with Number

4. Receiving return reference from a method that uses the type parameter - When receiving a reference from a method in a generic class that uses a type parameter as the return value, you need to handle it as the specific type provided to the generic class. Examples are listed below for the method "E firstElement()" in the class "java.util.Vector<E>":

   java.util.Vector<Number> verctorForNumber;
   ...
   Object firstTry = vectorForNumber.firstElement(); 
      // allowed - Return type Number can be up casted to Object
   
   Double secondTry = vectorForNumber.firstElement(); 
      // not allowed - Double is not compatible with Number

Last update: 2014.

Table of Contents

 About This Book

 Installing JDK 1.8 on Windows

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

Generic Classes and Parameterized Types

 What Is a Generic Class?

Using a Generic Class

 Using a Generic Class - Example

 Creating a Generic Class

 Creating a Generic Class - Example

 Bounded Type Parameters

 Raw Type, Generic Type and Parameterized Type

 Parameterized Type and Subtyping

 Wildcard Parameterized Types

 Wildcard Parameterized Type Test

 Wildcard Parameterized Subtyping

 Wildcard Parameterized Subtyping Example

 Generic Methods and Type Inference

 Lambda Expressions and Method References

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 Outdated Tutorials

 References

 PDF Printing Version