Kotlin Generics

Generic is a Kotlin feature that allows you to construct classes, methods, and properties that may be accessed by other types and that can be checked for all variations in classes, methods, and properties at the compilation time. In the article, we will discuss the concept of the generic class type in the Kotlin language. Generics provide types as an argument when declaring classes, interfaces, and methods. Like the more well-known instance variables given in method implementations, type arguments allow you to reuse code across multiple inputs.

What Is the Generic Type Class in Kotlin in Ubuntu 20.04?

Generics are powerful features that allow us to build classes, methods, and properties that may be accessed with various data types while maintaining compile-time type safety. A type is an object of a generic type with the specific type of arguments, and the generic type class or method is expressed as a parameterized type. Angle brackets can declare it, such as “<T>”. The “T” represents the class type and is commonly used in collections because they hold only one type of object and do not accept multiple objects.

How to Use the Generic Type Class?

To understand the basics of using the generics in Kotlin language, we have the following examples:

Example # 1: Program Without Generic Type Class in Kotlin:

We have created a simple class without declaring a generic type that takes data type parameters in a constructor.

In the previous Kotlin code, we created a simple class named “myString”. The class “myString” is declared with a primary constructor. The constructor has a single argument passed. Then, we have created an object of class “myString” and passed a data type “string” in the object. The main function has an object “name”, with a string value. The primary constructor accepts the specified string because it has a data type of string property. But in the case of passing integer value, then we have an error on the compilation type.

Example # 2: Program Using Generic Type Class in Kotlin:

The type parameter <T> is a placeholder for the type argument, which will be used in the generic class. When the generic class is instantiated, it will be replaced. By implementing the code, you will better understand how to use generic type classes.

In the previous Kotlin example code, we have created a class “Employee”. The class “Employee” is denoted with “<T>”, which means it is a generic type class. Then, the printEmployee function of the class “Employee” has an object “name” passed as a string type parameter. We have a Kotlin println function in the generic type class which will print the “emp” assigned as a single parameter of the class. We have stated the main function here. In the main function, we have created two objects of the class “Employee” as “emp” and “empAge”. The object emp is assigned with the string type, and the object empAge is assigned with Int type. These objects are initialized with the values called by the printEmployee() function of the class “Employee”.

Upon interpretation, we have the following code output shown on the console screen:

Example # 3: Program Using Out Keyword in a Generic Type Class in Kotlin:

Kotlin has introduced two keywords in generic class: “in” and “out” keywords. The given class can only produce the out value, but it cannot be consumed. Below, we have an illustration of using the “out” keyword:

In the previous code, we have a function represented as a “fun” keyword and named “copying”. The constructor is created for “copying”, which we have annotated the input argument with the out keyword to permit the compiler to compile our code. Then, we have copied the object “value” array to the “to” array by using them for function. The println function will print the elements of an array that we have copied. The main function has objects created as “intValue” and “anyVAlue”. The objects are assigned with “Int” and “Any” data types. Then, these objects are called by the class copying. The following output of the copied array from the code is shown on the terminal screen:

Example # 4: Program Using Star Projection in a Generic Type Class in Kotlin:

We use the asterisk (*) projection when we don’t know the exact type of the item and would like nothing more than to display all the array elements. Let’s have the following example to show how our star project works in the Kotlin program:

In the previous Kotlin example code, we have created a function named “myArray”. The class “myArray” has a primary constructor in which star projection is denoted as “<*>”. The star project “<*>” is used in an array to display the elements of an array. The class “myArray” has a foreach method defined to iterate over each element of an array. The print function is used by the foreach function and passes the keyword “it” as a single argument. Then, we have the main function defined in which we have created an object as “arrName”. The object “arrName” is initialized with the array elements using the arrayOf function. In the end, the object “arrName” is passed onto the class “myArray”.

Using the star project, we have successfully printed the following elements of an array shown as an output on the Kotlin compiler.


The article’s goal was to explain the concept in Kotlin on Ubuntu 20.04 in a very easy way. We started with a basic introduction to generics, then moved on to the different examples and created in various types of generic class ways. Generics is one of the types that can be used to accomplish the generic type’s operations called to the areas needed with the keyword and operators. We hope this article might be helpful for you. Check the other Linux Hint articles for more tips and information.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.