What will be printed as a result of compiling and running this code?

public class TestClass {
    public static void main(String[] args) {
        Base sub = new Sub();
        sub.test();
    }
}

class Base {
    public static void test() {
        System.out.println("Base.test()");
    }
}

class Sub extends Base {
    public static void test() {
        System.out.println("Sub.test()");
    }
}
Explanation
For static methods the late binding is uncharacteristically and the compiler selects the type based on the type of the link, and not the type of the assigned object.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Java quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes