Java casting resulting in run-time error instead of compilation error

前端 未结 7 900
感动是毒
感动是毒 2020-12-03 19:51

The following code snippet will result in a run-time:

class Vehicle {
    public void printSound() {
        System.out.print(\"vehicle\");
    }
}

class Ca         


        
相关标签:
7条回答
  • 2020-12-03 20:39

    This is a runtime error because you already defined the variable v as a Car. You cannot convert Car to Bike.

    The compiler will not check this kind of value assignment because compilers in general do not check semantics.

    0 讨论(0)
提交回复
热议问题