Share
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
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.
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 Tdef vehiclemodel(vm: T): Tval color : T}class Mercedes extends Vehicle {type T = Stringdef vehiclemodel(vm: String) = vmval color = "grey"}
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.”