Scala: downcasting throws java.lang.ClassCastException
Coming from a non-Java background to Scala has brought me a wide range of difficulties including this one. 

    scala> class A
    defined class A
    
    scala> class B extends A      
    defined class B
    
    scala> val a = new A
    a: A = A@2e893a4a
    
    scala> val b = new B
    b: B = B@3a47c130
    
    scala> a.asInstanceOf[B]
    java.lang.ClassCastException: A cannot be cast to B
    ...
    
    scala> b.asInstanceOf[A]
    res1: A = B@3a47c130

I understand that ClassCastException is thrown because at runtime, `b` doesn't seem like an A but in fact, it is (as far as I understand). What's going on here? Any workarounds? Thanks.
Scala: downcasting throws java.lang.ClassCastException
===

Coming from a non-Java background to Scala has brought me a wide range of difficulties including this one. 

    scala> class A
    defined class A
    
    scala> class B extends A      
    defined class B
    
    scala> val a = new A
    a: A = A@2e893a4a
    
    scala> val b = new B
    b: B = B@3a47c130
    
    scala> a.asInstanceOf[B]
    java.lang.ClassCastException: A cannot be cast to B
    ...
    
    scala> b.asInstanceOf[A]
    res1: A = B@3a47c130

I understand that ClassCastException is thrown because at runtime, `b` doesn't seem like an A but in fact, it is (as far as I understand). What's going on here? Any workarounds? Thanks.