Why do we use Generics?
- Stronger type check at compiler time.
- Avoid casting during reading the values.
- We can implement a generic algorithm for sorting String/Double/Integers
Example
-
Generic Class GenericClass.java
-
Generic Methods AppGM.java
-
Bounded Type parameters Bounded type parameters allow you to invoke methods defined in the bounds For Example
-
extends comparable -> we can use compare() method to compare items
-
a method that operates on numbers might only want to accept instances of Number or its subclasses.