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

前端 未结 7 903
感动是毒
感动是毒 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

    No. v is a Vehicle and it might be possible to cast it to Bike. It's not the compiler's job to figure out the actual runtime types of every object (especially because sometimes that's impossible).

提交回复
热议问题