What will be printed out as a result of the following code execution?

public class Main {

    private static class A1 {
        private String test() {
            return "A1";
        }
    }

    public static class A2 extends A1 {
        public String test() {
            return "A2";
        }
    }

    public static class A3 extends A2 {
        public String test() {
            return "A3";
        }
    }

    public static void main(String ... arg) {
        A1 a1 = new A1();
        System.out.println(a1.test());
        a1 = new A2();
        System.out.println(a1.test());
        a1 = new A3();
        System.out.println(a1.test());
    }

}
Explanation
Private methods are not inherited, they can not be overriden in descendant classes, late binding mechanism is not used for them. Therefore, test() method from A1 class will be always called - based on the type of a reference variable, not the actual object type.

Следи за CodeGalaxy

Мобильное приложение Beta

Get it on Google Play
Обратная Связь
Продолжайте изучать
тесты по Java
Cosmo
Зарегистрируйся сейчас
или Подпишись на будущие тесты