Java OCA OCP Practice Question 1551

Question

Which statement(s) about the following Thinker class are true?

package mypkg;//from  ww w . j  a v a  2  s  .c  o m
 
interface Human {
   default void write() {}
   static void publish() {}
   void think();
}
interface Wirter {
   public default void write() {}
   public static void publish() {}
   public void think();
}
 
public class Thinker implements Human, Wirter {
   @Override public void write() {}
   @Override public static void publish() {}
   @Override public void think() {
      System.out.print("Thinking...");
   }
}
  • I. The class fails to compile because of the write() method.
  • II. The class fails to compile because of the publish() method.
  • III. The class fails to compile because of the think() method.
  • A. I only
  • B. II only
  • C. I and II
  • D. II and III


B.

Note

The two interface definitions contain identical methods, with the public modifiers assumed in all interfaces methods.

For the first statement, the write() method is marked default in both interfaces, which means a class can only implement both interfaces if the class overrides the default method with its own implementation of the method.

Since the Thinker method does override write(), the method compiles without issue, making the first statement incorrect.

Next, the publish() method is marked static in both interfaces and the Thinker class.

While having a static method in all three is allowed, marking a static method with the @Override annotation is not because only member methods may be overridden.

The second statement is correct.

The think() method is assumed to be abstract in both interfaces since it doesn't have a static or default modifier and does not define a body.

The think() method is then correctly overridden with a concrete implementation in the Thinker class, making the third statement incorrect.

Since only the second statement was true, Option B is the correct answer.




PreviousNext

Related