Q:

scala.Some cannot be cast to custom object

I am having below code. I have started learning scala so might be there would be better approach to do these stuffs but I want to learn every bit of it. Please bear with me if code looks so naive.
  class ColaProduct() extends Product{
    override def productName = "Cola"
    override def productDetails = "Chilled Cola"
    override def toString(): String = super.toString()
  }

  class MilkProduct() extends Product{
    override def productName = "Milk"
    override def productDetails = "Healthy Milk"
    override def toString(): String = super.toString()
  }

  trait Machine {
    private val productMap = scala.collection.mutable.Map[String, Product]()
    def addProduct(product: Product): Unit ={
      productMap += product.productName.toString -> product
    }
    def checkAvl(name :String): Product ={
      if(productMap contains(name)){
        return productMap.get(name).asInstanceOf[Product]
      } else null
    }
    def process(name :String)
  }

  class VendingMachineImpl() extends Machine{
    override def process(name : String): Unit ={
      val product = checkAvl(name)
      if(null !=product){
        print("Got you :"+product.toString())
      }
    }
  }

  trait Product {

    private val defaultString: String = "Default"
    def productName = defaultString
    def productDetails = defaultString

    override def toString(): String = {
      return productName + " || " + productDetails
    }
  }

  def main(args : Array[String]): Unit ={
    val vendingMachineImpl = new VendingMachineImpl()
    vendingMachineImpl.addProduct(new ColaProduct)
    vendingMachineImpl.addProduct(new MilkProduct)

    vendingMachineImpl.process("Cola")
  }

Exception: 
Exception in thread "main" java.lang.ClassCastException: scala.Some cannot be cast to Product
    at vendingMachine$Machine$class.checkAvl(vendingMachine.scala:27)
    at vendingMachine$vendingMachineImpl.checkAvl(vendingMachine.scala:33)
    at vendingMachine$vendingMachineImpl.process(vendingMachine.scala:35)
    at vendingMachine$.main(vendingMachine.scala:47)

What I believed is once I define the map with specified type I don't have to match once retrieve the values from map. Is this understanding correct if not please let me know what is going wrong here. 

A:

If we look at the scaladoc for scala.collection.mutable.Map.get(), the method signature is described as:
 abstract def get(key: A): Option[B]

The method returns a value of type Option; if you try to cast it to some unrelated type, you will get a ClassCastException, as for any other incompatible types. If you want a product, you need to:

check that the Option isn't empty
unwrap the Product contained inside

Here's one way of doing it (without changing the rest of the code):
trait Machine {
  // Notes:
  // - no need to use 'return' keyword
  // - It's good practice in Scala not to return null: use Option for optional values
  def checkAvl(name :String): Option[Product] =
    productMap.get(name)
}

class VendingMachineImpl() extends Machine{
  override def process(name : String): Unit ={
    val product = checkAvl(name)
    // Think of Option as a collection containing 0 or 1 element: you can use
    // foreach, map, etc.
    // Also, string interpolation (with the s"" syntax) is cool. :-)
    product.foreach(print(p => s"Got you : $p"))
  }
}

Edit:
Also, in Scala, you can usually avoid explicit casts thanks to pattern matching. For example, if you want to explicitely unwrap the Product from the Option[Product], you can use:
val opt: Option[Product] = ???
opt match {
  // type-safe cast to type Some and deconstruction of the object (we get out the value
  // wich was originally passed to the Some constructor) :
  case Some(product) => print(s"Got you : $product")
  // type-safe cast to type None :
  case None => // do nothing
}

Edit 2:
you can also check out these two other methods for retrieving a value from a Map, depending on what you are trying to do:
val map: Map[String, String] = Map("k1" -> "v1", "k2" -> "v2")

// getOrElse (using a default value)
map.getOrElse("k1", "default") // returns "v1"
map.getOrElse("foobar", "default") // returns "default"

// apply (implementation-dependent, but fails fast by default)
map("k1") // returns "v1"
map("foobar") // throws a NoSuchElementException (it can be different for other Map
              // implementations, but it's the default behavior)

