public class TestClass {
public static void main(String[] args) {
B b = new C();
A a = b;
if (a instanceof A a1) a1.a();
}
}
class A {
void a(){ System.out.println("a"); }
}
class B extends A {
void b(){ System.out.println("b"); }
}
class C extends B {
void c(){ System.out.println("c"); }
}
Not sure what is your question. Do you mean why this question is there in our mock exams? It is because this topic is on the OCP 17 exam. The answer and the explanation provided in the s/w are as per Java 17.
If you like our products and services, please help us by posting your review here.
Not sure what is your question. Do you mean why this question is there in our mock exams? It is because this topic is on the real OCP 17 exam. The answer and the explanation are as per Java 17.
Thanks. You mentioned that it does not compile on 17, but on 21. If I understood your comment above.
That was in response to another user. It worked on their system most likely because they were using Java 21. It works on Java 21 because of the reason mentioned above. But the exam is for Java 17, so option 1 is incorrect.
If you like our products and services, please help us by posting your review here.