public class InheritanceInitOrder { static class A { String a; A() { a = "a"; System.out.println("a initialized"); System.out.println("b=" + ((B)this).b); //ВОТ ЗДЕСЬ } } static class B extends A { String b; B() { b = "b"; System.out.println("b initialized"); System.out.println("b=" + b); } } public static void main(String[] args) throws ClassNotFoundException { new B(); } }
В отмеченной строке объект класса A приводится к дочернему классу B. Разве так можно? Не могу понять!!!