When defining a variable of a generic type we can use a wild card instead of specifying the exact secondary type the generic type uses.

object HelloSample
  def main(args:Array[String]):Unit =
    val a:MyStack[SportCar] = new MyStack[SportCar];
    val b:MyStack[_ <: Car] = a;

class Car

class SportCar extends Car

class MyStack[T]

The following video clip shows the execution of this code sample and explains it.

Leave a Reply

Your email address will not be published. Required fields are marked *