Java inheritance downcast ClassCastException

后端 未结 6 1370
走了就别回头了
走了就别回头了 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:32

    All B's are A's, by inheritance. But not all A's are B's. This particular instance isn't, hence the runtime exception.

提交回复
热议问题