Java OCA OCP Practice Question 1446

Question

Which is the first line to fail to compile?

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

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


B.

Note

The Rectangle class is a subclass of the Shape class.

Since the use() method in Rectangle is intended to override the one in Shape, there are certain rules.

One is that the access modifier must not be more specific.

Option B is correct and r2 is the only line with a compiler error in this code.




PreviousNext

Related