Generics were added by JDK 5. Most of the algorithms are logically same irrespective of the type of data
they are being applied to. By using generics you can define an algorithm once, and you can apply it on any kind of
datatype without any additional effort.
At very high level, generics are nothing but parameterized types. Generics helps us to create a single class, which can be
useful to operate on multiple data types. A class, interface or a method that operates on a parameterized type is called generics
class, interface or method. Generics adds type safty. Remember that generics only works on objects, not primitive types. Here you
can find many examples to create generic classes, generic methods, and generic interfaces.
Java Generics Sample Code Examples
- Write a simple generics class example.
- Write a simple generics class example with two type parameters.
- How implement bounded types (extend superclass) with generics?
- How implement bounded types (implements an interface) with generics?
- What is generics wildcard arguments? Give an example.