Java inheritance downcast ClassCastException

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

    Remember that legal casts are ruled by the "IS-A" test, why you can compile your code?, because ClassCastException is an Unchecked Exception, since extends from RuntimeException

    ClassCastException ---IS-A--> RuntimeException (Another example of a possible legal cast).

提交回复
热议问题