Open In App

Generic Classes in Scala

Last Updated : 25 Apr, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

In Scala, forming a Generic Class is extremely analogous to the forming of generic classes in Java. The classes that takes a type just like a parameter are known to be Generic Classes in Scala. This classes takes a type like a parameter inside the square brackets i.e, [ ]. This classes are utilized explicitly for the progress of the collection classes in Scala. The sub-typing of generic types is invariant i.e, if we have two list types, A and B then List[A] is sub-type of List[B] if and only if type B is equivalent to type A.
Some points to remember:

  • The symbol used for a type parameter of a simple type is A, like List[A].
  • The symbols used for a type parameter of second, third, fourth, and so on, types in generic classes are respectively B, C, D, and so on.
  • The symbol used for a key is A and for a value is B in Scala Map.
  • The symbol used for a numeric value is N.

Note: Though this symbol conventions are provided, still any symbol can be used for the type parameters.
let’s discuss some examples below.
Example:




// Scala program of forming 
// Generic classes
  
// Creating an object
object GfG
{
  
    // Main method
    def main(args: Array[String]) 
    {
  
        // Class structure for Generic
        // types 
        abstract class Divide[z] 
        {
            // Defining method 
            def divide(u: z, v: z): z
        }
  
        // Extending Generic class of 
        // type parameter Int 
        class intDivide extends Divide[Int] 
        {
            // A method returning Int 
            def divide(u: Int, v: Int): Int = u / v
        }
  
        // Extending Generic Class of 
        // type parameter Double
        class doubleDivide extends Divide[Double] 
        {
            // A method returning Double
            def divide(u : Double, v : Double) : Double = u / v
        }
  
        // Creating objects and assigning 
        // values to the methods called
        val q = new intDivide().divide(25, 5)
        val r = new doubleDivide().divide(21.0, 5.0)
  
        // Displays output 
        println(q)
        println(r)
      
    }
}


Output:

5
4.2

Here, the abstract class Divide has a type parameter z, and it is present in the square brackets so the class is generic type, this type parameter z can take up each and every data types. we have defined a method divide inside the Generic class which has two variables i.e, u and v and the data type of these variables is z. The class intDivide, as stated above take up integer types and the class doubleDivide, as stated above take up double types. Thus, the type parameter z was replaced by Int and Double data types. By this way sub-typing is possible in Generic classes.

Example :




// Scala program of using generic
// types for numeric values
import Numeric._
  
// Creating an object
object GfG
{
  
    // Main method
    def main(args: Array[String]) 
    {
  
        // Defining generic type for numeric
        // values with implicit parameter 
        def addition[N](a: N, b: N)(implicit num: Numeric[N]):
  
        // Using a method 'plus' 
        N = num.plus(a, b)
  
        // Displays the sum of two 
        // numbers
        println("The sum is : "+addition(88, 12))
    }
}


Output:

The sum is : 100

Here, we saw generic type parameter for numeric values and so, we have used symbol N here though any symbol can be used as a type parameter for a generic types.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads