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
// 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)
}
}
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
// 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))
}
}
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.
Similar Reads
Abstract Classes in Scala Abstraction is the process to hide the internal details and showing only the functionality. In Scala, abstraction is achieved by using an abstract class. The working of the Scala abstract class is similar to Java abstract class. In Scala, an abstract class is constructed using the abstract keyword.
5 min read
Abstract types vs Generics in Scala In this discussion, we will explore abstract types and generics in Scala, highlighting their differences. Abstract types are types that include abstract members, while generics are types that can take other types as parameters. Let's delve into the disparities between these two concepts. Scala Abstr
2 min read
Scala | Value Classes Value classes are a new mechanism which help to avoid allocating run time objects. AnyVal define value classes. Value classes are predefined, they coincide to the primitive kind of Java-like languages. There are nine predefined value types : Double, Float, Long, Int, Short, Byte, Char, Unit, and Boo
2 min read
Determine the class of a Scala object To determine the class of a Scala object we use getClass method. This method returns the Class details which is the parent Class of the instance. Below is the example to determine class of a Scala object. Calling method with argument - Example #1: Scala // Scala program to determine the class of a S
2 min read
Type Casting in Scala A Type casting is basically a conversion from one type to another. In Dynamic Programming Languages like Scala, it often becomes necessary to cast from type to another.Type Casting in Scala is done using the asInstanceOf[] method. Applications of asInstanceof methodThis perspective is required in ma
4 min read