Oracle Certified Professional Java Se 11 Developer · Free Practice Question Hard
Question 47
Question ID: UKOCP27389
Given code:
- package com.udayankhattry.ocp;
- class Logger {
- public void log() {
- System.out.println("GO FOR IT");
- }
- }
- public class Test {
- public static void main(String[] args) {
- Logger obj = new Logger() {
- public void Log() {
- System.out.println("LET IT BE");
- }
- };
- obj.Log();
- }
- }
What will be the result of compiling and executing Test class?
- A Compilation error
- B Runtime error
- C GO FOR IT
- D LET IT BE
Reveal correct answer
Correct answer: A
Explanation
UKOCP27389:
Anonymous inner class allows to define methods not available in its super class. Anonymous inner class in this case doesn't override the method of super class but it provides a new method Log [L in upper case].
obj is of Logger type, obj.Log(); statement tries to invoke the Log() method of Logger class, which is not available and hence it causes compilation error.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
You must be logged in to post a comment.
