Practice mode Java

Given class A { static String s() { return “A”; } } class B extends A { static String s() { return “B”; } }, what does A o = new B(); System.out.println(o.s()); print?

Correct answer D. A

Explanation

Static methods are hidden rather than overridden. A call made through an expression is bound at compile time using its static type, here A, and the object is ignored, so A is printed. The code compiles, though IDEs commonly flag it, and writing A.s() would make the real meaning obvious. Dynamic dispatch applies only to instance methods.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.