Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

scala

What is an Abstract Type Member in Scala?

Dian Us Suqlain

If a member has an incomplete definition in the class, it is abstract. In Scala, we can declare abstract type members in classes, traits, and subclasses that can offer concretea subclass of an abstract class that implements all its abstract method types.

The implementation of abstract members should be written inside its subclasses. If a class contains an abstract member, the class itself must also be declared abstract.

Scala provides abstraction through both abstract member types and values.

Code

Take the following abstract methods and their implementation in a concrete subclass. Observe how an incomplete type T is declared where its type is unknown.

trait Vehicle {
  type T
  def vehiclemodel(vm: T): T
  val color : T	
}

class Mercedes extends Vehicle {
  type T = String 
  def vehiclemodel(vm: String) = vm
  val color = "grey"
}
Abstract Member Type - Scala

Explanation

Method vehiclemodel() returns type T and member vehicle model (vm) of type T. Here we can say that T is an abstract type.

Another class, Mercedes, extends the Vehicle class and defines type T as String. The vehiclemodel() method displays the model of the car. The color variable is initialized to “grey.”

RELATED TAGS

scala

CONTRIBUTOR

Dian Us Suqlain
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring