Generic Classes and Parameterized Types
This chapter provides tutorial notes and example codes on generic classes and parameterized types. Topics include what is a generic class; what is a type parameter; using parameterized types and subtyping; using wildcard parameterized types and subtyping.
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
- A generic class is a class that supports type parameters with different behaviors when given different types on invocation.
- A generic class has a signature of ClassName<A, B, C, ...>.
- Type parameters can be bounded as ClassName<T extends C & I1 & I2...> to ensure strong typing to reduce runtime type mismatches.
- A parameterized type is a reference type for generic class objects with argument types provided.
- Subtyping of parameterized types is based on their generic class hierarchy, like class Hashtable<K,V> extends Dictionary<K,V>.
- Raw types for generic types introduced in JDK 1.5 are supported for backward compatibility.
- A wildcard parameterized type is a parameterized type with argument types containing wildcards for subtyping purposes.
- There 3 categories of wildcard parameterized types: unbounded, upper bounded and lower bounded.
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
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
PDF Printing Version