When we define a function or a class and we choose to use generics we can set limits on the unknown type:
S <: T – means that S is a sub type of T S >: T – means that S is a super type of T
It is also possible to mix between a lower bound and an upper one:
S >: T1 <:T2 – S must be a super type for T1 and a sub type for T2.
The following code sample shows an example for setting limits, both an upper one and a lower one, on the unknown type.
object HelloSample { def main(args:Array[String]):Unit = { val ob = getMyStack[SportCar](10) } def getMyStack[T >: RacingCar <: Car](num:Int) = { new MyStack[T](num) } } class MyStack[T](size:Int) { //... } class Car class SportCar extends Car class RacingCar extends SportCar
The following video clip shows the execution of this code sample, overviews it and provides more in-depth understanding for this topic.