Generics feature is template kind of implementation approach, which allow us to define classes, methods and properties which are accessible using different data types and keep a check of the compile-time type safety.
A generic type is a class or function that is parameterized over types. We always use angle brackets (<>) to specify the type parameter in the program.
Compile time safety: Generics code is checked at compile time for the parameterized type so that it avoids run time error
Type casting: No need to typecast the object.
Type safety: Generic allows only single type of object at a time.
If you ever defined generic in Kotlin, you’ll notice many a times, it would propose to use the in or out keyword to define the generic. It puzzles me at a start on when which is used, and for what.
Formally, this is a way to define Covariant & Contravariance. Let’s see with the example in simple Word Processing and its implementation on Consumer (User) & Producer (Output Mode).
Out (Covariant): If your generic class/interface only use the generic type as output of its function/s, it won't allow us to add the setter function with T