Java OCA OCP Practice Question 1492

Question

Which is the first line to fail to compile?

class Shape { 
   default void use() { }     // r1 
} 

class Rectangle extends Shape { 
   public void use() { }  // r2 
   public void bang() { }  // r3 
} 
  • A. r1
  • B. r2
  • C. r3
  • D. None of the above


A.

Note

While there is a default keyword in Java, it is only allowed in interfaces or in switch statements.

It is not a visibility modifier.

The author of this code probably intended for the method to be package-private, which doesn't use a visibility modifier.

The line with default doesn't compile, so Option A is correct.

If default was removed, the code would all compile.




PreviousNext

Related