Example # 01
Within our first example of Scala programming, we will be discussing the most basic code to understand generic classes. You need to ensure that your Linux operating system already has a Scala language package installed at your end. So let’s start by initiating the console terminal application of our Linux system first. It is necessary as we have to create a new Scala file in it. Thus, we have tried Linux’s “touch” instruction at its query area with the title of a Scala file to be created. The Scala file must be created with the “.scala” extension to avoid any issues. On using the “ls” instruction, we can see the “test.scala” file in the list as well.
It’s time to add Scala code to the newly built one. But, before that, you have to open your file within some editor. We prefer to use the text editor in the Linux system as the text editor is very easy and convenient to use. Therefore, the test.scala file has been launched in the text editor using a manual way, i.e., using file explorer. Now, you need to understand one thing the keyword “generic” is used for not only classes but also Scala types as well.
So, we will be using the generic type for parameters of a simple function. Started this example with the import of the Numeric library in the code, i.e., to use generic types of numeric values. The object has been created at the next line with the name “test” that would be utilized for execution purposes. It contains the definition of a main() function responsible for the overall execution of the Scala program. In this driver code, we have defined a function subtraction with a generic type “N” to be used for numeric values “x” and “y” via the implicit parameter or argument. As the generic type has been specified, you can provide any type of numeric value for the calculation of a subtraction result. The very next line contains the implicit numeric variable “n” to call the built-in “minus” function and takes “x” and “y” numeric values as parameters.
The result would be saved to the “N” generic term. The println() statement would display the subtraction function result by passing it any two numeric values. The subtraction() function is only taking the generic type of variables, and the minus function is calculating the result and returning it to the calling println() statement here. Let’s save our code before execution.
As we know, all the programming languages do have their compilers to compile and make the code bug-free. So, the scala has its compiler “Scalac”. We have used the “test.scala” file in the scalac query to compile it. The name of an object “test” from the file has been utilized for the execution of the compiled object code. In return, we have got the subtraction result “-60” as a generic type for numeric values passed to the subtraction function.
Example # 02
Let’s dig a little more to understand generics. Within this example, we will be elaborating on the use of generic types for classes now. Only the same thing left in the file is the object name, i.e., test. The main() function definition starts its execution by defining some classes in it first. The generic class it contains is also the abstract class named “Multi” using the generic parameter “z”. This class contains a definition of a function “multiply” using the generic type “z” for its parametric values “u” and “v”.
A sub-class “iMultiply” has been defined here that has been extending the “Multi” generic class with an “Integer” type, i.e., using the “extends” keyword. It also uses the same name function “multiply” in it that has been using the Integer type for its values to calculate the multiplication result “u*v”. Another class, “dMultiply” also extends the generic class “Multi”, but with the Double type to calculate multiplication result “u*v” via its same name function “multiply” extended from the abstract class. After that, passed integer values to the multiply() function of the iMultiply class that has been extending the main abstract class “Multi”. The calculated result would be saved to a new variable, “im”.
Similarly, we have initialized a variable “dm” taking back the multiplication result from the multiply() function of the dMultiply class after passing it 2 double values. In the last two lines of code, we have been utilizing the variables “im” and “dm” in the println() function statement to display the integer and double multiplication results.
On compilation of the file, no bugs have been found so far. Executed it with the scala instruction and got the integer and double multiplication results, respectively.
Conclusion
That’s all for using the generic types for classes and their functions in Scala. We have explained the use of generic types in Scala very briefly in the introduction. After that, we have discussed two very basic and easy-to-understand examples to elaborate on how you can use generic types to inherit and use the generic parent classes in the child classes of specified types. The concept of generic types is quite convenient to use when it comes to inheritance.