×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

Abstract types vs Generics in Scala

By IncludeHelp Last updated : October 26, 2024

Scala Abstract Type

Abstract types have abstract members i.e. their members do not have the definition or the values. Traits and abstract classes are abstract type variables.

Example

Here's an example demonstrating the use of abstract types in Scala to create flexible and type-safe containers:

abstract class Container {
  type A  // Abstract type member

  def add(element: A): Unit  // Method that uses the abstract type
  def get(): A  // Method to get an element of type A
}

// Concrete Implementation
class IntContainer extends Container {
  type A = Int  // Specify the concrete type

  private var elements: List[A] = List()  // Use the concrete type

  def add(element: A): Unit = {
    elements = element :: elements
  }

  def get(): A = elements.head  // Return the first element
}

object AbstractTypeExample {
  def main(args: Array[String]): Unit = {
    val intContainer = new IntContainer()
    intContainer.add(10)
    intContainer.add(20)
    println(intContainer.get())
  }
}

Scala Generics

Generic classes are those classes that have classes or other types as parameters.

Example

This example demonstrates using generics in Scala to perform division operations on different data types:

object CalculatorApp { 

  // Main method 
  def main(args: Array[String]): Unit = { 

    // Abstract class for generic types 
    abstract class Operation[T] { 
      // Defining method 
      def calculate(a: T, b: T): T 
    } 

    // Extending generic class with Int type parameter 
    class IntOperation extends Operation[Int] { 
      // A method returning Int 
      def calculate(a: Int, b: Int): Int = a / b 
    } 

    // Extending generic class with Double type parameter 
    class DoubleOperation extends Operation[Double] { 
      // A method returning Double 
      def calculate(a: Double, b: Double): Double = a / b 
    } 

    // Creating objects and assigning values to the methods called 
    val result1 = new IntOperation().calculate(40, 8) 
    val result2 = new DoubleOperation().calculate(45.0, 9.0) 

    // Display output 
    println("Result of IntOperation: " + result1) 
    println("Result of DoubleOperation: " + result2)
  } 
}

Difference Between Abstract Types and Generics

The differences between abstract types and generic classes in Scala are as follows:

Feature Abstract Types Generics
Definition Implemented or extended in a Scala program Takes a class type as the parameter
Method Definitions Methods do not have definitions Methods have definitions and work with other types
Relation Type Uses a "has-a" relation Uses an "of" relation
Example Baleno is a type of Car Array of maps (Array[Map[String, Int]])

Combining Abstract Types and Generics

In Scala, an abstract class or a trait can be abstract type whereas a class that is abstract or simple can be generic if it accepts any type.

A class can be abstract as well as generic also, as both concepts can work together as well as individually. You can create a Scala class that generic and abstract as:

abstract class product[x] {
  def product(a:x , b:x ): x 
}

The method to this class has no definition which means it is abstract and the class uses data of type x which means it is a generic one.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.