Java inheritance downcast ClassCastException

后端 未结 6 1375
走了就别回头了
走了就别回头了 2021-01-22 17:10

given the following code, I have a question:

class A{}
class B extends A {}
class C extends B{}

public class Test {
    public static void main(String[] args) {         


        
6条回答
  •  清歌不尽
    2021-01-22 17:27

    Downcasts are illegal. An A is not a B and therefore you can't cast it to be one. You can cast B to be A, but not the other way round.

提交回复
热议问题